Laravel
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?
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.
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.
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:
The steps involved in establishing a socket on the server side are as follows:
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”);
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 UsThe technology has been a pioneer in speeding up the data analysis of customers for enterprises.
17 Jul 2024
Learn about Alfresco development and its benefits for businesses. Discover how leveraging Alfresco can enhance efficiency and collaboration.
17 Jul 2024
Mastering Alfresco Development: Essential Tips for Success in ECM Solutions” provides crucial insights and strategies for developers to excel in…
17 Jul 2024
401, One World West, Nr. Ambli T-Junction 200, S P Ring Road, Bopal, Ahmedabad, Gujarat 380058
Kemp House 160 City Road, London, United Kingdom EC1V 2NX
Nürnberger Str. 46 90579 Langenzenn Deutschland
Level 36 Riparian Plaza, 71 Eagle Street, Brisbane, QLD 4000
4411 Suwanee Dam road, Bld. 300 Ste. 350 Suwanee GA, 30024
Cube Work Space, 24 Hans Strijdom Avenue, Cape Town
B 503 Sama Tower, Sheikh Zayed Road, United Arab Emirates
34 Applegrove Ct. Brampton ON L6R 2Y8
We use cookies to improve your browsing experience.
OKAYThis website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.