Socket.IO

Socket.IO: What Is It & Why Do You Need It?

Socket.IO

Are you ever surprised how we get live scores in Cricbuzz and get WhatsApp messages without refreshing the page or doing any event? 

It is possible just because of the socket. Now you might be thinking, what is a socket? 

What is a Socket?

Sockets in computer networks are used to allow the transmission of information between two processes of the same machines or different machines in the network.

Sockets allow communication between two different processes on the same or different machines. To be more precise, it’s a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file, and it can be a network connection, a text file, a terminal, or something else.

To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes.

What Socket.IO?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. 

The main idea behind Socket.IO is that you can send and receive any events you want with any data you want. Any objects that can be encoded as JSON will do, and binary data is supported too.

When do we need to use it?

Whenever data updating frequency is very fast that time we use sockets like sports score, share market share price etc.

Some examples of real-time applications are −

Instant messengers − Chat apps like WhatsApp, Facebook Messenger, etc. You need not refresh your app/website to receive new messages.
 

Push Notifications − When someone tags you in a picture on Facebook, you receive a notification instantly. 

The steps involved in establishing a socket on the client side are as follows:

  1. Create a socket with the socket() system call
  2. Connect the socket to the address of the server using the connect() system call
  3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.

The steps involved in establishing a socket on the server side are as follows:
 

  1. Create a socket with the socket() system call
  2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
  3. Listen for connections with the listen() system call
  4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
  5. Send and receive data

How to use it in the backend:-

first, install socket.io using the command:-

npm i socket.io
 

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', socket => {
socket.on('event', data => { /* … */ });
socket.on('disconnect', () => { /* … */ });
});
server.listen(PORT_NAME);

 


If you want to send a message to everyone except for a certain emitting socket, we have the broadcast flag for emitting from that socket: 

 

io.on('connection', (socket) => {
socket.broadcast.emit('hi');
});

 


In this case, for the sake of simplicity, we’ll send the message to everyone, including the sender.
 

  io.emit('chat message', msg);

Sockets work based on events. There are some reserved events that can be accessed using the socket object on the server side.
 

These are −

Connect 
Message
Disconnect
Reconnect
Ping
Join and
Leave.
 

The client-side socket object also provides us with some reserved events, which are −

Connect
Connect_error
Connect_timeout
Reconnect, etc.

// to all connected clients
io.emit("hello");
// to all connected clients in the “news” room
io.to(“news”).emit(“hello”);

 

Conclusion

Updating data in real-time can be tricky if your application demands that frequently. However, Socket.IO enables low-latency communication between the client and the server to offer that event-based communication. 

For applications that require instant messaging and push notifications, Socket.IO makes perfect sense. If you also want similar future-ready solutions for your project, our Digital Experience Solutions can help you.
Get in touch with us today!

   

Transform Your Business With Digital Enterprise Solutions

Contact us

Our Offices

INDIA AHMEDABAD, INDIA

401, One World West, Nr. Ambli T-Junction 200, S P Ring Road, Bopal, Ahmedabad, Gujarat 380058

UK
UK

Kemp House 160 City Road, London, United Kingdom EC1V 2NX

GERMANY GERMANY

Nürnberger Str. 46 90579 Langenzenn Deutschland

AUSTRALIA AUSTRALIA

Level 36 Riparian Plaza, 71 Eagle Street, Brisbane, QLD 4000

USA USA

4411 Suwanee Dam road, Bld. 300 Ste. 350 Suwanee GA, 30024

SOUTH AFRICA SOUTH AFRICA

Cube Work Space, 24 Hans Strijdom Avenue, Cape Town

UAE DUBAI, UAE

B 503 Sama Tower, Sheikh Zayed Road, United Arab Emirates