These examples illustrate how the WebRTC technologies are working. For a better understanding, I decided to split the development into several steps.
npm install
You can optionally specify the running step and the port with STEP
and PORT
variables:
export STEP=2 # 3 by default
export PORT=8088 # 8080 by default
Launch the server:
npm start
Concerning adapter.js
:
adapter.js
is a shim to insulate apps from spec changes and prefix differences.
It ensures the uniformity of WebRTC functionalities through all browsers.
This is the heart of the communication process, inspired by this example. The communication is only inside one page. We can see the Session Description Protocol for the offer and the answer. There is no communication through the server, it's only inside the browser's page.
localConnection = new RTCPeerConnection
remoteConnection = new RTCPeerConnection
createDataChannel
onicecandidate - handle ice candidate
remoteConnection.addIceCandidate(event.candidate)
localConnection.createOffer -> offer
localConnection.setLocalDescription(offer)
remoteConnection.setRemoteDescription(offer)
remoteConnection.createAnswer -> answer
remoteConnection.setLocalDescription(answer)
localConnection.setRemoteDescription(answer)
Implementation of socket.io for the client-server
communication. This works only when 2 users are connected. Each user's id
is
given by the server to respect unicity. As all variables are stored in memory,
only one server at a time can be launched, and every restart will clear all
variables.
Manage a list of connected users. The server keeps track of every socket with
clientSockets
.
All opened connections are stored on the client side peerConnectionById
.
Hence, once the connection is created for sending a message, we wouldn't have to
reinitialize the connection for backward communication.
npm test
To run tests locally, you need chromium-browser.
For linux, follow these steps: http://stackoverflow.com/a/24364290/4388775.
For windows, follow these steps: http://stackoverflow.com/a/26561341/4388775.
Tests are inspired by webRTC utilities. I have to remove firefox beta and unstable plus chrome unstable to make it pass with node version 6. sleep can't be built on Travis.
According to socket.io:
It works on every platform, browser or device, focusing equally on reliability and speed.
Supposing it's in parallel, it appears that socket.io can support up to about 1800 simultaneous connections with the server. Otherwise, SocketCluster observes 42K concurrent users from a single machine.
Finally, Daniel Kleveros claims to have handled 600k concurrent websocket connections. For me, it really depends on the use case and the messages per second per user but I don't see technical limitation.
To support more users, we could partition our user set by chosen groups. In real case, it's unlikely that every user needs potentially to connect to any user because they will not watch the same video for example. Hence, with a persistence load balancing strategy, every request of the same user can be sent to the same server.
Creating a stun server from scratch is a hard problem to tackle. However you can use this open project mainly written in C and follow the install instructions.