Modern JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Creating your own PeerServer

Before we start building a chatroulette, let's see how to run our own instance of PeerServer.

PeerServer is available as an npm package on npm cloud. Let's create a custom PeerServer and use it with the PeerJS application that we built in our previous chapter.

First create a directory named Custom-PeerServer and place app.js and package.json files in it.

In the package.json file, place the following code and run the npm install command to download the PeerServer package:

{
  "name": "Custom-PeerServer",
  "dependencies": {
    "peer": "0.2.8",
    "express": "4.13.3"
  }
}

At the time of writing, the latest version of PeerServer is 0.2.8. Here, we will also download the express package, as we will need to demonstrate how to integrate PeerServer with Express.

PeerServer package provides a library to create a custom PeerServer or integrate PeerServer with Express and also an executable file to directly create our own instance of PeerServer without any customization.

Run PeerServer from shell

If you want to directly run your own instance of PeerServer from shell without any customization, then run the following command in the Custom-PeerServer/node_modules/peer/bin directory:

./peerjs –port 8080

It should now print the following command:

Started PeerServer on ::, port: 8080, path: / (v. 0.2.8)

This confirms that PeerServer is running. To test whether the PeerServer instance is working or not, go to the index.html file of the application that we created in our previous chapter and replace the following code:

peer = new Peer(id, {key: ""});

The preceding code will be replaced with the following code:

peer = new Peer(id, {host: "localhost", port: 8080});

Now run the application and it should work as usual.

Using PeerServer library

PeerServer library is used to create a custom PeerServer. The PeerServer library also allows us to integrate PeerServer with the Express server.

Creating custom PeerServer

Here is an example code that demonstrates how to create your own custom PeerServer. Place the following code in the app.js file and run the node app.js command to start the server:

var PeerServer = require("peer").PeerServer;
var server = PeerServer({port: 8080});

server.on("connection", function(id) {
  console.log(id + " has connected to the PeerServer");
});

server.on("disconnect", function(id) {
  console.log(id + " has disconnected from the PeerServer");
});

Here, the first two lines of the code create the custom PeerServer. We then attached event handlers that will be triggered when a user connects or disconnects from PeerServer. A custom PeerServer doesn't provide an API to check whether a peer is allowed to connect to PeerServer or not. It just allows us to do something after the peer is connected or when the peer disconnects.

To test whether the custom PeerServer is working or not, go to the index.html file of the application that we created in the previous chapter and replace the following code:

peer = new Peer(id, {key: ""});

The preceding code will be replaced with the following code:

peer = new Peer(id, {host: "localhost", port: 8080});

Now run the application and it should work as usual.

Integrating PeerServer with the Express server

We can also integrate PeerServer with the Express server, that is, a particular path of the Express server will provide the signaling service. The main advantage of integrating PeerServer with the Express server is that we can check whether a peer is allowed to connect to PeerServer or not, and if it is not allowed, then we can stop the peer from using it.

Here is an example code that demonstrates how to integrate PeerServer with the Express server. Place the following code in the app.js file and run the node app.js command to start the server:

var express = require("express");
var app = express();

var server = app.listen(8080);

app.use("/signaling", function(httpRequest, httpResponse, next){
  //check whether peer is allowed to connect or not.

  next();
});


var ExpressPeerServer = require("peer").ExpressPeerServer(server, {debug: true});

app.use("/signaling", ExpressPeerServer);

ExpressPeerServer.on("connection", function(id){

});

ExpressPeerServer.on("disconnect", function(id){

});

Here we are using a middleware provided by the PeerServer library to integrate PeerServer with Express. Here, PeerServer is made available on the /signaling path. You can use any path you want to.

The PeerServer library doesn't provide any way to check whether the peer is allowed to connect to PeerServer or not, so we are using our own technique, that is, we are attaching another middleware on top of the ExpressPeerServer middleware, which performs this check. Although this technique may seem fine, if our custom middleware stops the request from proceeding further, then PeerServer fires the connection and disconnect events and destroys the Peer instance on the frontend.

Note

You can learn more about PeerServer at https://www.npmjs.com/package/peer.