Nadeem Ahmad
Nadeem Ahmad

Reputation: 745

Socket IO doesn't get emitted from client - Node JS

I am working on Socket IO, the connection between the client and the server is established successfully. I am facing two problems:

1 - When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue / problem while communicating with the server or even with the client using sockets, while not having the same ids ? or does the id on the server get changed when the client page is refreshed ?

2 - On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.

Socket Connections

function Globals() {

  this.socketConnection = async function() {
    let p = new Promise(function(res, rej) {
      io.on("connection", function(socket) {
        if (socket.connected) {
          res(socket);
        } else {
          rej("Socket Connection Error !");
        }
      })
    })
    return await p;
  }
}

new Globals().socketConnection().then(function(soc) {
  console.log(soc.client.id);
  socket = soc;
  soc.emit("Hi");
  soc.on("Nady", function() {
    console.log("I am called");
  })
})

Client Side Connection

function Globals() {
  this.socketConnection = async function() {

    var socket = io('http://localhost:8080');

    let p = new Promise(function(res, rej) {
      socket.on('connect', function() {
        if (socket.connected) {
          console.log(socket.id);
          res(socket);
        }
      })
    })

    return await p;
  }
}

var socket;
new App().socketConnection().then(function(s) {
  socket = s;
});

function ScrapJobs() {

  var socket;
  new App().socketConnection().then(function(s) {
    socket = s;
  });
  var _this = this;

  this.attachListeners = function() {
    qs("#init-scrap").ev("click", _this.startScrapping);
  }

  this.startScrapping = function() {
    console.log("I am cliced");
    socket.on("Hi", function() {
      console.log("Hi Nadeem");
    })
    socket.emit("Nady");
  }

}

Upvotes: 0

Views: 80

Answers (1)

jfriend00
jfriend00

Reputation: 708116

When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue

The client side socket.id value is set on the client socket object after the connect event is received and is updated (e.g. modified) upon a reconnect event.

It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).

On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.

You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.


A general comment about your code. Both code blocks you show appear to be stuffing a socket object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.

Upvotes: 1

Related Questions