romain-cotte / webRTC-examples

0 stars 0 forks source link

Messaging system

Build Status Dependency Status

These examples illustrate how the WebRTC technologies are working. For a better understanding, I decided to split the development into several steps.

Installation

npm install

Start the server

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.

Step 1

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.

Step 2

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.

Step 3

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.

Tests

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.

What percentage of users is supported ?

According to socket.io:

It works on every platform, browser or device, focusing equally on reliability and speed.

How many users can connect to one server ?

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.

How can the system support more users ?

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.

How can I create my own stun 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.

References

  1. WebRTC adapter - github.com
  2. WebRTC utilities - github.com
  3. A Dead Simple WebRTC Example - shanetully.com
  4. Session Description Protocal - wikipedia
  5. WebRTC connectivity - developer.mozilla.org
  6. Using multiple nodes
  7. 600k concurrent websocket connections on AWS using Node.js
  8. Maximum concurrent Socket.IO connections