O'Reilly logo

Node.js Blueprints by Krasimir Tsonev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing user-to-user communication

Our chat is now functioning, but it would be nice if we could send a message to one specific user. Such a feature requires changes in both places: at the frontend and backend. Let's first change the Node.js script.

Changing the server-side code

So far, the users were anonymous in our system. We just passed the received message to all the sockets available. However, to implement a user-to-user conversation, we need to set unique ID for every user. Along with that, we have to keep references to all the created sockets so that we can emit messages to them. This can be done as follows:

var crypto = require('crypto');
var users = [];

We can make use of the crypto module, which is available by default in Node.js to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required