Is it possible to use WebRTC Data Channels on Node.js in a way that mimics the functionality (and preferably API) of Socket.io (WebSockets) except using UDP?
In essence I want to have a server running Node.js with which browser clients can establish a full duplex bi directional UDP connection via JavaScript.
There is a WebRTC module for node.js: https://js-platform.github.io/node-webrtc/
The installation can be really cumbersome (to say the least) but if you succeed you'll be able to make your node.js server act as a WebRTC peer just as browsers do. This way you'd be able to open a Data Channel between a browser and your node.js server.
We have this in use in our research project to evaluate performance characteristics.
Yes, in theory you should be able to to do this. However, you'll need a node module that supports WebRTC data channels, so that you can connect to it like any other peer. Unfortunately, scanning through the current modules, I don't see one that implements the data channel.
The other thing to note is the WebRTC data channels can be configured for different reliability modes with the reliable mode being implemented with SCTP (Firefox and Chrome).
When testing the data channels I would recommend using Firefox since the WebRTC work was split such that Chrome initially focused on PeerConnection and Firefox on DataChannel (so Firefox is still ahead in their implementation of the DataChannel).
Related
My current use case is that I'm trying to mock a system that uses WebRTC for live video streaming (for a robot). This way, I don't have to be connected to the robot to develop the client.
My issue as of now is that I have no idea how to stream a video using WebRTC to connected peers. I've seen many examples of how to do this from client to client using a signaling server, but other than directly sending the video buffer using socket.io, I haven't seen an example of server -> client WebRTC streaming.
I'm planning to use Node.JS for mocking the video stream as I've been using it for the rest of the robot's systems.
It really isn't that different though client to client or server to client. You want to stream/broadcast a video to all the connected peers. Think of your server will be a client in the setup.
You can also use a WebRTC solution like Janus Repo it is a simple gateway and completely open source. Refer to - WebRTC & Dev API's for more info.
If you find latency issues after peers have increased in number you can check - Mesh, Routing, Multi peer architecture for some solutions for it.
Hope it helps.
My question is about whether I can connect to peerjs without a browser.
So if or how I can create a WebRtc video and data peer to peer connection between a device that send
and a device that can see the video through the browser.
So far I have a well working WebRtc connection from one browser to the other.
I have a website using NodeJS, a peerjs server as well as a stun and turn server running and I connect using the peerjs api
using the javascript code that is executed in the browser.
However, I would like to connect on one side without a browser, for example to make it more efficient on a Raspberry pi.
I found this example which provides a python port for peerjs, but it is very hard to understand and poorly documented.
https://github.com/ambianic/peerjs-python
I was able to connect to my peerjs server with this. Now I can't get any further.
I would appreciate any suggestions to solve this problem or any other ideas or examples to establish a connection using peerjs without a browser. Maybe with c++ or Nodejs.
You can use a native WebRTC library like libdatachannel. The examples show how to connect browsers to native clients, and you might adapt them to be compatible with the PeerJS signaling protocol. It features a C and C++ API but there is also a Node.js wrapper.
What I have :
A C++ application server running, Ready to send data to client which is supposed to a HTML5 page or app.
What I want : Is there any way to communicate using udp port with HTML5 given both c++ server and HTML5 app are local to system ?
What I know :
Because of Security Concern, JS doesn't allow UDP port communication from browser.
Have read in many places, Answer is no. But answers are old.
Is the answer still 'NO' ?
Is there any work-around possible ?
Any lead is appreciated.
Yes, the answer is still 'no'. Websockets are TCP based. Note that a WebSocket is not a plain TCP connection, there is HTTP negotiation and a framing protocol in place. So you also cannot create a plain TCP connection in Javascript.
WebRTC is based on UDP, it may cover your use cases: http://www.html5rocks.com/en/tutorials/webrtc/datachannels/
Chrome now seems to have something: https://developer.chrome.com/apps/sockets_udp
It looks like UDP for web is still an active area of development and potential standards creation. Posting this answer to record some new info current as of May 2020.
The following whitepaper has outlined a potential path forward that satisfies the security needs for an "unreliable-unordered" protocol: https://gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/
There are extensions to desktop Chrome and desktop Firefox that are in active development.
https://github.com/RedpointGames/netcode.io-browser
The way mobile browsers are designed prevents this kind of modification from being added at present (good security reasons again) but could be added down the road.
This is a major issue for gamers. See that link for a discussion of websockets, webrtc, quic (in chrome), and the author's netcode.io
You could alternatively create an additional python local server for bridging the data between your C++ application and webpage.
The html5 webpage connects to a local port that allows a web socket connection (use Flask/tornado).
The C++ application connects to a UDP listener on a different port. See https://wiki.python.org/moin/UdpCommunication to setup.
The python server basically forms a transparent data bridge between UDP port to websocket connection .
After reading all the links and comments, we can conclude:
NO, YOU CAN'T SEND THE UPD PACKAGE FROM THE BROWSER.
And you probably won't, because adding such a feature would be a giant leap backwards in web security.
You could possibly use a work around, design a program/script/server(I would use PHP, being a html client) to get the UDP gram from the server, if you would like I could help, I have worked on something similar.
I'm new to WebRTC, actually just heard about it a few days ago and I've read a lot about it. However, I still have a few questions.
What do I need to explore the usage of WebRTC? E.g.: do I need a server, any libraries etc.? I'm aware that new version of Chrome and Firefox support WebRTC, but besides these two browsers, is there anything else that is necessary?
What is the main purpose of WebRTC when addressing practical usage? To video chat? Audio chat? What about text-chatting?
Does WebRTC need a server for any kind of browser-to-browser interaction? I've seen some libraries, such as PeerJS that don't explicitly mention any kind of server... so is it possible to connect two clients directly? There's also a PeerServer, which supposedly helps broker connections between PeerJS clients. Can I use WebRTC without such a server?
What are the most commonly used libraries for WebRTC?
What's a good starting point for someone who's totally new in WebRTC? I'd like to setup a basic google-talk kind of service, to chat with one person.
Thank you so much guys.
You can find many docs here E.g. this one, this one and this one!
You can find a few libraries here.
A simple multi-user WebRTC app needs following things:
Signalling server to exchange sdp/ice/etc. ---- e.g. socket.io/websockets/xmpp/sip/XHR/etc.
ICE server i.e. STUN and/or TURN; to make sure Firewalls doesn't block UDP/TCP ports
JavaScript app to access/invoke RTCWeb JavaScript API i.e. RTCPeerConnection.
It just takes a few minutes to setup WebRTC peer-to-peer connection. You can setup peer-to-server connections as well where media-servers can be used to transcode/record/merge streams; or to relay to PSTN networks.
WebRTC DataChannels can be used for gaming, webpage synchronizing; fetching static contents, peer-to-peer or peer-to-server data transmission, etc.
What do I need to explore the usage of WebRTC? E.g.: do I need a
server, any libraries etc.? I'm aware that new version of Chrome and
Firefox support WebRTC, but besides these two browsers, is there
anything else that is necessary?
WebRTC it is JavaScript API for web developers which can be used for audio and video streaming.
But there are 2 notices:
You need a signaling path.
For example, if your first user is Alice using Firefox and second user is Bob using Chrome,
they should negotiate used codecs and streams.
WebRTC does not offer the signalling implementation. So you need to implement the signaling yourself. It is quite simple. You need to send SDP(stream config) to participant and receive an SDP answer. You can use plain HTTP via apahe server or use Websockets or any other transport to negotiate SDP.
So, it seems you need an intermediary signaling server workning with websockets or HTTP/HTTPS.
Once you negotiated the streams you are sending your audio or video stream, but the distanation user might have a simmetric NAT. It means that you stream will not be delivered to the target user. In such situation you need a TURN server to traverse the NAT.
Finally you will need 2 server-side logic items:
1) Signaling server
2) TURN or proxy server
To start, take a look Web Call Server.
The server implements HTML5 Websocket signaling and SRTP proxying as a TURN server.
You can also learn the webrtc application open source code.
First steps:
1. Download the signaling and streaming server.
2. Download and unzip web client.
3. Start the web client and debug javascript code to learn more how webrtc works.
To clarify when I ask about browser to browser communication I mean without a server in between forwarding message. I would like to implement something like this for a game. If p2p in websockets isn't possible are there similar alternatives? Any help is appreciated.
No. Browsers can only initiate WebSockets connections, not receive them. The W3C browser API spec only defines how to start an outbound connection.
You can make an application that would both initiate and accept WebSockets connections, but browsers do not do this.
You might look at Pusher App which you could use to build a WebSockets application with multiple clients. The service provides up to 20 simultaneous WebSockets clients for free (you can pay for higher scaling).
Update:
The WebRTC protocol and API is making rapid progress and allows a Data Channel to be established between two peers (you still may need a STUN/TURN server for the initial NAT traversal and setup).
In theory it is possible with WebRTC DataChannel:
RTCDataChannel is a WebRTC API for high performance, low latency,
peer-to-peer communication of arbritary data. The API is
simple—similar to WebSocket—but communication occurs directly between
browsers, so RTCDataChannel can be much faster than WebSocket even if
a relay (TURN) server is required (when 'hole punching' to cope with
firewalls and NATs fails).
"In theory" because it isn't supported by a stable browser yet and you still need a relay server (TURN) if one of the browsers is behind a symmetric NAT. Nevertheless, it is a really promising feature.
Update: Chrome 26 and Firefox 22 support RTCDataChannel by default and Firefox 19-21 if you enable WebRTC by setting media.peerconnection.enabled to true (about:config).
I was reading about websocket and peer 2 peer and found PeerJS.
I still haven't made anything though, but by the examples it looks promising.
Now days it's possible, currently only Chrome,FF and Opera support it (desktop).
There's some libraries starting to pop up around the web right now, such as PeerJS and js-platform-p2p which pretty much simplifies things.
Simple and reliable cross browser supported way is to use http://httprelay.io with AJAX calls. It is also implements one to many communication what could be useful for game development.