Categories
JavaScript Answers

How to send additional data on socket connection with JavaScript socket.io?

Spread the love

To send additional data on socket connection with JavaScript socket.io, we call the emit function with the data.

For instance, we write

socket.on("connect", () => {
  socket.emit("hello", data);
});

io.on("connection", (client) => {
  client.on("hello", (data) => {});
});

to call emit to emit the 'hello' event with the data we want to send to the client.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *