DEV Community

Sushanta Gupta
Sushanta Gupta

Posted on

3

How to send and receive data in Socket IO?

Socket.io is a library to abstract the WebSocket connections. According to the official website of socket io, Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server.

What does it mean by event-based? We can send and receive data in Socket by using the following code block. We can send an event from the server and receive it in the client and vice-versa.

To Receive Data
socket.on("message", (hello) => {
console.log(hello); // world
});

To send Data
socket.emit("message", "hello");

In general, the code for sending and receiving data is
Receiving Data: socket.on(eventName, listenerFn)
Sending Data : socket.emit(eventName, Data)

We have to use same eventName for sending and catching data. There are some pre-defined events in Socket Io but we can also make our own custom events provided that the same event name is to be used in both client and server.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more