Different Ways to Send Message Using Sockets to another Process - An example from Node Application

Introduction 

We all are aware that to develop complex application the best way to design it is to create loosely coupled modular designs with distributed logic. This way it will be more Scalable and maintainable in the future.

Often in those scenarios there is a need to communicate between different processes running in the application to proceed with its operation. There are various IPC(Inter Process Communication) mechanisms that are there to accomplish this like use of shared memory, using pipes etc. What we are going to discuss here is about sockets which are web based to communicate between different processes. The advantage of using Sockets are they enable two way communication between processes and its event based.

Example of Socket Programming

We will see an example from the Node application how this is done, mainly we will be concentrating on the different ways or types of firing message using sockets.

importing sockets library

As you can see in the example above from Node js server code we will be importing the sockets.io node package. Using this package we will be creating socket instance to fire messages to clients.

Creating Socket instance

The above example shows how to create a socket instance using the http Server.

Basically there are two ways we can use sockets to interact with other process. One is using the <socket instance>.emit('<Event type>',<Actual message>); This is used to emit the message identified by the event string, which client uses to listen to that event to react to it. Therefore in the Server it can be used as shown below:

Emit message using Sockets

Here we are emitting the message using the emit API. Therefore in the client side where the other end point of the socket is will be as follows:

Listen to the Socket Server

In the client side we have to use the on  API to act on any messages sent by the server. One point to note here that we have to use the same event name as used in the server side other wise it would not work.

There are other ways to transmit messages using sockets such as broadcast the message to all the clients etc. This can be achieved by using the socket.broadcast api as shown below:

Socket Broadcast

This will notify all the clients except the sender of the message as it is normally used inside the socket.on message handler lambda function. If we want to notify all the connected clients then we have to use the io.emit API.

There is one more variation to it like if we want to send the message to the group of people identified by a room name in the case of Chat Application. This can be done using the socket.to(<Room name>) API as follows:

Emitting message to Room users


One point to note here that both emitting and listening to message can both be done at the server and the client code as it is bidirectional.


Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples