Skip to content

aljanabim/simple_webrtc_signaling_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple WebRTC Signaling Server

WebRTC is an evolving technology for peer-to-peer communication on the web. To establish a succesfull WebRTC connection, the peers need to exchange ICE candidates and session description protocol (SDP). This can be done using any method of data transport. Once that connection is established, the peers no longer need to stay connected to the signalling server. Read more here. In this repository, we will use socket.io to implement the simplest possible signaling logic.

Table of Contents

Installation

Make sure to have both Node.js and Yarn installed.

git clone https://github.com/aljanabim/simple_webrtc_signaling_server.git
cd simple_webrtc_signaling_server
yarn install

Usage

The signaling server can be used in development and in production. You can use it locally to develop your WebRTC logic or deploy it on the web using your deployment service of choice (see Deployment) see Deployment. Be sure to checkout Simple WebRTC Node.js Client for client implemenation that is compatable with the API of this signaling server.

Development

For development signlaing server make sure to update the environment variables, in /config/dev.env, according to your desires. Then run:

yarn dev

Production

For a production signaling server, run

yarn start

Examples

For an actual implementation of a Node.js WebRTC client that utilizes the API of this signaling server, checkout Simple WebRTC Node.js Client. Otherwise, you can find starter code for a signaling channel which interacts with the signaling server in /examples/signaling-channel.js. To see an example of how a client might utilize the signaling server as a signaling channel, run

yarn client1

and in another terminal window run

yarn client2

you will see console logs of what the siganling server and the clients are doing, according to the API (see below). All the code for the examples is found in the /examples directory.

Deployment

You can deploy this signaling server on whichever deployment service you are comfortable with, eg. Heroku, GCP, AWS, Azure, Vercel, Netlify, etc. In this section we will see an example of how to deploy the signaling server on Heroku. Follow these steps:

  1. Do the steps in the Prerequisites section in this article and come back once you are done.

  2. In a terminal window run each of the following lines separately (ie. don't copy-paste all the commands at once)

    heroku login
    heroku create [name]
    git push heroku main
    heroku open

    The [name] argument allows you to give your signaling server a unique name. You can leave out the [name] argument to let Heroku generate a random name.

  3. Save the URL you get in the new browser window, which opens once you run heroku open, and use it as the SIGNALING_SERVER_URL in /examples/signaling-channel.js or Simple WebRTC Node.js Client.

API

The signaling server listens to the following events

  1. "connection": Fired when a client sucessfully connects to the server.

  2. "disconnect": Fired when the client disconnects from the server. When fired, the message is broadcasted to all other peers in the following format:

    {
        from: peerId, // the client that fired the `ready` event
        target: "all", // all other clients are informed
        payload: { action: "close", message: "Peer has left the signaling server" },
    }
  3. "message": Fired when a client sends a message to the server. When fired, the message is broadcasted to all other clients in the following message format

    {
        from: peerId, // id of sender
        target: "all", // sent to all other peers,
        payload: message, // the content of the message
    }
  4. "messageOne": Fired when a client emits a messageOne event. When fired, the message is broadcasted only to the client that is specified in the message target. The message has the following format

    {
        from: peerId, // id of sender
        target: targetPeerId, // id of recieving peer,
        payload: message, // the content of the message
    }
  5. "ready": Fired when a client emits a ready event. When fired, the client peerId and socketId are added to the object of connections in the server. Furthermore, a message is broadcasted to all already connected clients in the following format:

    {
        from: peerId, // the client that fired the `ready` event
        target: "all", // all other clients are informed
        payload: {
            action: "open",
            connections: [newPeer],
            bePolite: true
        }
    }

    and the connecting client recieves information about the exising connections in the following format:

    {
        from: "all", // all other conneted clients
        target: peerId, // the newely connected client
        payload: {
            action: "open",
            connections: Object.values(connections),
            bePolite: false
        }
    }

Connections API

The server also exposes an API Endpoint which return a JSON object containing all the connections registered in the server. It can be accessed on [SIGNALING_SERVER_URL]/connections. Ie. whichever URL is used to indicate where signaling server is hosted, concatenated with "/connections". For localhost at port 3000, this becomes http://localhost:3000/connections.


Best of Luck with your WebRTC adventures. If you have any feedback, don't hestitate to reach out ☺.

About

A WebRTC signaling server implemented in Node.js with Socket.io

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published