Tutorial

Installation

Generating self signed certificate for development

WebRTC is not allowed in browsers unless your connection is encrypted, inbox runs in HTTPS mode by default given that you have certificate files in the current directory.

Lets generate certificate files.

openssl genrsa -des3 -passout pass:secretpassword -out server.pass.key 2048
openssl rsa -passin pass:secretpassword -in server.pass.key -out server.key
openssl req -new -key server.key -out server.csr
openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt

When you’re asked about any information no need to enter any of it, also don’t add a password/secret phrase to the certificate, remember this is for you on your development machine.

Running the server

Accessing the server

Writing you WebRTC application from scratch

Signal Javascript class

To use inbox in your javascript application you can write a small class that asks the server for new messages or send messages to another peer with his ID.

The following snippet will send a message to a peer by his user name:

async send(server, from, password, to, data) {
  const response = await fetch(`${server}/inbox?to=${to}`, {
    method: 'POST',
    cache: 'no-cache',
    headers: new Headers({ 'Authorization': 'Basic ' + window.btoa(from + ":" + password) }),
    body: JSON.stringify(data)
  });
}

Inbox will get the message and will create an inbox for the from user with password provided if the inbox doesn’t exist.

To send a message to another user this user has to exist on the server, this is why every user should first start by asking the server about any new messages, then send a message to another user if he wants to.

The following function should allow you to get the latest message from the server

async receive(server, from, password) {
  const response = await fetch("${server}/inbox", {
    method: 'GET',
    cache: 'no-cache',
    headers: new Headers({ 'Authorization': 'Basic ' + window.btoa(from + ":" + password) }),
  });

  try {
    var data = await response.json();
    return data;
  } catch(e) {
    return null;
  }
}

This will get the latest message (the server will not respond until there is a message, if inbox is empty it will wait until it gets a message from the other end) the server will create an inbox for the user with the provided username and password

You’ll need to keep polling the server for new message as long as you expect new messages from peers, if you are connected to all expected peers then no need to ask the server anymore.

There is no technical limitation that stops you from continueing to poll the server even after connecting to your peers, but it will consume server resources so it’s a good practice to be conservative with your requests to allow the server to serve as many users as possible.

Your javascript code now can use these two functions to send and receive message from other peers in conjunction with RTCPeerConnection javascript class to generate WebRTC offers/answers and send it to the other peer until the connection is established, you can check /public/webrtc.js for an example of how to do that.