Emitting a private message to another socket

Sometimes, you need to send a private message to just one other socket and not every socket that may be listening in. As the server side is in charge of managing all the connected sockets, we can specify the sockets that our events are emitted to granularly.

In this recipe, we will use this ability to create a simple app. Here, the user can specify which user to give a hug to. Only the giver and receiver will be aware that the hug was initiated. Our application will look something similar to the following screenshot:

Emitting a private message to another socket

Getting ready

For this recipe, we will use jQuery for the DOM manipulation and Bootstrap ...

Get Socket.IO Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.