Selecting signaling protocol for video/audio app (WebRTC API) - javascript

Signaling plays an important role in WebRTC but is not standardized, allowing the developer to choose. This lack of standardization and multiple options has resulted in some confusion. A number of different signaling approaches have been proposed and used, and an understanding of the differences between the approaches is useful in selecting the right one for a given WebRTC application. So the questions arrise
How to select a protocol for signaling between the server and end-points in a
P2P media intensive app like video chat app?
WebRTC DataChannel vs XMLHttpRequest vs WebSockets ? Maybe something else?
How and when to choose one over another?

Signaling is the process of setting up a session between two parties. Because DataChannel depends on having a peer connection already established, you cannot use it for signaling (at least not for the initial setup). Although if you really wanted to, you could implement a DataChannel signaling solution to handle any later changes in configuration, like if one client wants to add another video stream. This post discusses this more in-depth. Whether or not the effort involved to build and maintain this is up to you to decide.
HTTP and WebSockets will both work fine in this scenario, and I don't think one would offer any significant advantage over the other. WebSockets is technically lighter, but the signaling process is brief and doesn't involve much data transfer.
I'd recommend using something like socket.io, which abstracts away the differences between the two protocols and automatically handles upgrading and downgrading.

Related

Is there a way to connect two browsers without WebRTC?

I want to create a Peer to Peer connection with two browsers without using existing code (mostly). I want to implement the server infrastructure by myself, as well as the client code.
There is just one issue, WebRTC seems to be everywhere.
Don't get me wrong, I'd use it, but since this is for a school project I have to implement almost everything by myself.
Looking at the WebRTC source code, I expected some Javascript implementations of existing components, however, all I ended up seeing is very complex C++ code that is intended for web browser developers.
Is it possible to implement a Peer to Peer connection between two browsers without using WebRTC?
For security reasons browsers do not allow you to make UDP and TCP requests yourself. You need to use one of the 3 protocols provided by browsers
HTTP
WebSockets
WebRTC
The C++ code you saw is the underlying implementation that browsers can use for WebRTC

Data from database real-time. Use websockets or not? [duplicate]

I'm interested in building a small real-time multiplayer game, using HTML5/JavaScript for the client and probably Java for the server software.
I looked into WebSockets a bit, but it appears I had misconceptions on what WebSockets actually are. I had initially thought of WebSockets as just JavaScript's way of handling TCP sockets, just as they are used in Java and other languages, but it appears there is a whole handshaking process that must take place, and each transmission includes much HTTP overhead (and in that case, the benefits over Ajax do not seem as exciting as at a first glance)?
On a related topic, are there any better alternatives to WebSockets for this purpose (real-time multiplayer games in JavaScript)?
WebSockets are the best solution for realtime multiplayer games running in a web browser. As pointed out in the comments there is an initial handshake where the HTTP connection is upgraded but once the connection is established WebSockets offer the lowest latency connection mechanism for bi-directional communication between a server and a client.
I'd recommend you watch this: https://www.youtube.com/watch?v=_t28OPQlZK4&feature=youtu.be
Have a look at:
http://browserquest.mozilla.org/ code available here: https://github.com/mozilla/BrowserQuest
https://chrome.com/supersyncsports/
The only raw TCP solution would be to use a plugin which supports some kind of TCPClient object. I'd recommend you try out WebSockets.
You can find a number of options here. Just search for WebSockets within the page.
Also take a look at WebRTC. Depending on the purpose of your game and whether you need your server to manage game state, you could use this technology for peer-to-peer communication. You may still need a solution to handle putting players into groups - in that case WebSockets is the fastest/best solution.
Basically, you have 3 options at the time of this writing:
WebSockets
WebSockets is a lightweight messaging protocol that utilizes TCP, rather than a Javascript implementation of TCP sockets, as you've noted. However, beyond the initial handshake, there are no HTTP headers being passed to and fro beyond that point. Once the connection is established, data passes freely, with minimal overhead.
Long-polling
Long-polling, in a nutshell, involves the client polling the server for new information periodically with HTTP requests. This is extremely expensive in terms of CPU and bandwidth, as you're sending a hefty new HTTP header each time. This is essentially your only option when it comes to older browsers, and libraries such as Socket.io use long-polling as a fallback in these cases.
WebRTC
In addition to what has been mentioned already, WebRTC allows for communication via UDP. UDP has long been used in multiplayer games in non web-based environments because of its low overhead (relative to TCP), low latency, and non-blocking nature.
TCP "guarantees" that each packet will arrive (save for catastrophic network failure), and that they will always arrive in the order that they were sent. This is great for critical information such as registering scores, hits, chat, and so on.
UDP, on the other hand, has no such guarantees. Packets can arrive in any order, or not at all. This is actually useful when it comes to less critical data that is sent at a high frequency, and needs to arrive as quickly as possible, such as player positions or inputs. The reason being that TCP streams are blocked if a single packet gets delayed during transport, resulting in large gaps in game state updates. With UDP, you can simply ignore packets that arrive late (or not at all), and carry on with the very next one you receive, creating a smoother experience for the player.
At the time of this writing, WebSockets are probably your best bet, though WebRTC adoption is expanding quickly, and may actually be preferable by the time you're done with your game, so that's something to consider.
I'm not sure if WebSockets are still the best tool for networking
a real-time multiplayer these days (2017). WebRTC is a newer technology
which offers the potential of much higher performance. And these
days, WebRTC is also easier to work with thanks to the following libraries:
node-webrtc simplifies server-side networking
webrtc-native which also provides a server-side library, and could be faster as its name suggests
electron-webrtc provides an implementation which is a good match if you want to package your game using electron
Alternatively, if you want to be spared the actual details of networking implementation, and you're looking for a library which provides a higher-level multiplayer interface, take a look at Lance.gg. (disclaimer: I am one of the contributors).
Multiplayer games requires the server to send periodic snapshots of the world state to the client. In the context of a browser HTML/js application you have little choices: polling, websocket or write your own plugin to extend browser capabilities.
The HTTP polling such as BOSH or Bayeux are sophisticated but introduces network overhead and latency. The websocket was designed to overcome their limitation and is definitely more responsive.
Libraries, such as cometd or socket io, provide an abstraction of the transport and solve the browser compatibility issues for you. On top of that, it allows to switch between the underlying transports and compare their performance without effort.
I coded multiplayer arcade game with socket.io and usual measure 2ms latency with a websocket and around 30ms with xhr-polling on lan. It's enough for a multiplayer games.
I suggest you to have a look to nodejs and socket.io in order to be able to share code between the client and the server, you also car borrow some multiplayer code at [3].
If you are planing to use JavaScript for your game (as you are) then WebSocket is the best choice for you. And if you want to support older version of Internet Explorer then think of Signal R system Microsoft developed. They are using WebSocket under the hood, but they also have a few fall back options...so protocol will use the best available solution available.
http://signalr.net/

Making a node.js application a PEER with WebRTC

So, I have a web app that generates large buffers of color information that I want to send to a node application running on another machine in my local network. Web Sockets doesn't seem to be fast enough for me. I was looking to use UDP and it seems WebRTC is the only way to do that from a browser. The caveat, it seems, is WebRTC is only PEER to PEER (browser to browser). I figured, I could use node webkit to emulate being my other "PEER". In my node app I could handle the "signaling" and have it set itself up in a RTCPeerConnection to my web app. Therefore, I could send my data from my web app to my node app (local network). For some context, I have one computer running native software to drive a light fixture and I want to use a web app to control the lights.
To boil the question down, how can I make a RTCPeerConnection from a browser to a node webkit app?
Any help would greatly appreciated.
Thank you!
-Jake
Node-RTCPeerConnection is an attempt (current WIP) to create a spec compliant implementation of RTCPeerConnection for Node.js entirely in JavaScript with no native C or C++ code. This enables browser-peers to speak to non-browser (Node.js) peers.
But you can not use it for production yet.
Then we also have wrtc (node-webrtc) that provides a native module for NodeJS that supports a subset of standards-compliant WebRTC features. Specifically, the PeerConnection and DataChannel APIs.
Too many people are having problems with wrtc. Since it has to download lots of source and build it only to find out that it fails after a long while on certain platforms. Unfortunately it doesn't come with any prebuilt packages described in this issue
You can use either the google implementation of webrtc or a more recent implementation (by Ericsson) called openWebrtc. The developers of openWebRTC are very proud of running their implementation on various pieces of hardware like raspberry pi and iOS devices.
The one that worked best for me was electron-webrtc (which in turn uses electron-prebuilt) for better compatibility. It creates a hidden Electron process (which is based on Chromium, so WebRTC support is great!) and communicates with that process to enable WebRTC in Node.js. This adds a lot of overhead.
It is intended for use with RTCDataChannels, so the MediaStream API is not supported.
Other resources:
https://github.com/webrtcftw/goals/issues/1
Update 2019
Currently, the best and easiest way to solve this problem is to use webrtc module. Check samples for inspiration. This module does what you were looking for, implemented with N-API and using Canvas module to compose new video from the client stream. Hopefully this will help those who face this problem in the future.

Use node.js as a WebRTC peer?

What modules are in existence to use node.js as a peer in WebRTC? I'd like to use WebRTC in a more client/server fashion than P2P for its apparent ability to send packets unreliably. (AKA, I don't want the huge delay TCP makes by guaranteeing packet arrival with data in order)
If I have to use a stripped-down browser page as a server, that would perhaps work... however, it would really be sub-optimal. Node.js would make things much smoother, and probably more reliable too.
Thanks!
Have a look at the Erizo component of Licode (WebRTC MCU). It has a stream controller and webrtc controller written in c++ with a js interface. It might help you getting a idea or two.
There is now a Node implementation of WebRTC, with the exception of MediaStreams.
https://github.com/js-platform/node-webrtc
There is a c++ interface for WebRTC. WebRTC is based on the libjingle project but uses the JSEP (Javascript Session Establishment Protocol) instead of XMPP for sending STUN/TURN information for NAT tunneling. The two projects were in the process of being merged when I looked at this a while back so compiling/linking it was a PITA. This may have been improved last year.
The goal would be to expose the native API for WebRTC as a node module with the node addon api and package it as an npm module that works like the in-browser API. The cross-browser polyfill will show you how it should look.
There's a lot of cool stuff you could do with this (call recording, SIP connectors, .torrent extensions to the browser, etc.) I really encourage you to try this!
The most relevant package i've found was http://js-platform.github.io/node-webrtc/ i managed to build it and play with it a little bit... The developer is very helpful, i think it's your best bet right now
The solution is to use libjingle or licode/erizo. Both of them require compilation but erizo provides a NodeJS interface. Libjingle was created by Google.
Unfortunately, you have to compile each library and there are no binary packages for Debian, Ubuntu or other platforms.
Take a look at PeerJS: Simple peer-to-peer with WebRTC.
You need PeerJS-server for signaling.
The guide: http://peerjs.com/
I used Node js with socket io and have success with it
There are many tutorials online

Are WebSockets suitable for real-time multiplayer games?

I'm interested in building a small real-time multiplayer game, using HTML5/JavaScript for the client and probably Java for the server software.
I looked into WebSockets a bit, but it appears I had misconceptions on what WebSockets actually are. I had initially thought of WebSockets as just JavaScript's way of handling TCP sockets, just as they are used in Java and other languages, but it appears there is a whole handshaking process that must take place, and each transmission includes much HTTP overhead (and in that case, the benefits over Ajax do not seem as exciting as at a first glance)?
On a related topic, are there any better alternatives to WebSockets for this purpose (real-time multiplayer games in JavaScript)?
WebSockets are the best solution for realtime multiplayer games running in a web browser. As pointed out in the comments there is an initial handshake where the HTTP connection is upgraded but once the connection is established WebSockets offer the lowest latency connection mechanism for bi-directional communication between a server and a client.
I'd recommend you watch this: https://www.youtube.com/watch?v=_t28OPQlZK4&feature=youtu.be
Have a look at:
http://browserquest.mozilla.org/ code available here: https://github.com/mozilla/BrowserQuest
https://chrome.com/supersyncsports/
The only raw TCP solution would be to use a plugin which supports some kind of TCPClient object. I'd recommend you try out WebSockets.
You can find a number of options here. Just search for WebSockets within the page.
Also take a look at WebRTC. Depending on the purpose of your game and whether you need your server to manage game state, you could use this technology for peer-to-peer communication. You may still need a solution to handle putting players into groups - in that case WebSockets is the fastest/best solution.
Basically, you have 3 options at the time of this writing:
WebSockets
WebSockets is a lightweight messaging protocol that utilizes TCP, rather than a Javascript implementation of TCP sockets, as you've noted. However, beyond the initial handshake, there are no HTTP headers being passed to and fro beyond that point. Once the connection is established, data passes freely, with minimal overhead.
Long-polling
Long-polling, in a nutshell, involves the client polling the server for new information periodically with HTTP requests. This is extremely expensive in terms of CPU and bandwidth, as you're sending a hefty new HTTP header each time. This is essentially your only option when it comes to older browsers, and libraries such as Socket.io use long-polling as a fallback in these cases.
WebRTC
In addition to what has been mentioned already, WebRTC allows for communication via UDP. UDP has long been used in multiplayer games in non web-based environments because of its low overhead (relative to TCP), low latency, and non-blocking nature.
TCP "guarantees" that each packet will arrive (save for catastrophic network failure), and that they will always arrive in the order that they were sent. This is great for critical information such as registering scores, hits, chat, and so on.
UDP, on the other hand, has no such guarantees. Packets can arrive in any order, or not at all. This is actually useful when it comes to less critical data that is sent at a high frequency, and needs to arrive as quickly as possible, such as player positions or inputs. The reason being that TCP streams are blocked if a single packet gets delayed during transport, resulting in large gaps in game state updates. With UDP, you can simply ignore packets that arrive late (or not at all), and carry on with the very next one you receive, creating a smoother experience for the player.
At the time of this writing, WebSockets are probably your best bet, though WebRTC adoption is expanding quickly, and may actually be preferable by the time you're done with your game, so that's something to consider.
I'm not sure if WebSockets are still the best tool for networking
a real-time multiplayer these days (2017). WebRTC is a newer technology
which offers the potential of much higher performance. And these
days, WebRTC is also easier to work with thanks to the following libraries:
node-webrtc simplifies server-side networking
webrtc-native which also provides a server-side library, and could be faster as its name suggests
electron-webrtc provides an implementation which is a good match if you want to package your game using electron
Alternatively, if you want to be spared the actual details of networking implementation, and you're looking for a library which provides a higher-level multiplayer interface, take a look at Lance.gg. (disclaimer: I am one of the contributors).
Multiplayer games requires the server to send periodic snapshots of the world state to the client. In the context of a browser HTML/js application you have little choices: polling, websocket or write your own plugin to extend browser capabilities.
The HTTP polling such as BOSH or Bayeux are sophisticated but introduces network overhead and latency. The websocket was designed to overcome their limitation and is definitely more responsive.
Libraries, such as cometd or socket io, provide an abstraction of the transport and solve the browser compatibility issues for you. On top of that, it allows to switch between the underlying transports and compare their performance without effort.
I coded multiplayer arcade game with socket.io and usual measure 2ms latency with a websocket and around 30ms with xhr-polling on lan. It's enough for a multiplayer games.
I suggest you to have a look to nodejs and socket.io in order to be able to share code between the client and the server, you also car borrow some multiplayer code at [3].
If you are planing to use JavaScript for your game (as you are) then WebSocket is the best choice for you. And if you want to support older version of Internet Explorer then think of Signal R system Microsoft developed. They are using WebSocket under the hood, but they also have a few fall back options...so protocol will use the best available solution available.
http://signalr.net/

Categories

Resources