Home > Mobile >  Is it possible to send some data on connect to the server in socket.io?
Is it possible to send some data on connect to the server in socket.io?

Time:11-25

I want to send the username on connection (the moment we connect) to the server, something like this :

const socketConnect=io("http://localhost:3000","myusername")

Is something like this possible ?

CodePudding user response:

It's not possible. See the client API documentation here.

What you have to listen to is the connect event and then send your username:

const socket=io("http://localhost:3000")
socket.on("connect", () => {
    socket.emit("login", userData, (serverReply) => {
      console.log("Server's response", serverReply);
    });
    // ...
});

Hope this is helpful.

  • Related