Tuesday 23 May 2017

Emit events not firing in sockets.io

I have an app that has two users. User 1 sends commands to User 2 via the server. The server receives the message from User 1 and sends it out to User 2. Easy!

I have a working version, but it needs to be changed because I need to add new events. The old emitting events received by the server all work, but - mystery! - when I add new ones they don't. The messages are DEFINITELY being sent by User 1, I did extensive investigation. It seems that the new emit events just aren't working, despite the syntax being identical.

Server code:

io.sockets.on("connection", function (socket) {
    console.log("hello");

    socket.on("disconnect", function () {
        console.log("bye");
    });

  // This one works fine! 
  socket.on("start_test_broadcast", function(socket){
    console.log("Server received START_TEST message from the remote. Broadcasting ...");
    io.emit("start_test_broadcast");
  });

 // This one works too!
  socket.on("end_test_broadcast", function(socket){
    console.log("Server received END_TEST message from the remote. Broadcasting ...");
    io.emit("end_test_broadcast");
  });

  // This is a new one - doesn't work!
  socket.on("start_p1_broadcast", function(socket){
    console.log("Server received P1_START message from the remote. Broadcasting ...");
    io.emit("p1_broadcast", {'hello' : 'world'});
  });

Here's User 1, that's initiating the events:

$(".start_test").on("click", function() {
    // This sends the message to the server:
    socket.emit("start_test_broadcast", {'message': 'There has been a START_TEST received'});
    console.log("I am the remote and I sent a START_TEST command!");
});

$(".end_test").on("click", function() {
    // This sends the message:
    socket.emit("end_test_broadcast", {'message': 'There has been a END_TEST received'});
    console.log("I am the remote and I sent a END_TEST command!");
});

// New event - this fires, console.log() all works:
$(".start_p1").on("click", function() {
    // This sends the message:
    socket.emit("start_p1_broadcast", {'message': 'There has been a STARTP1 received'});
    console.log("I am the remote and I sent a START_P1 command!");
});

And here's User 2, which should be receiving and responding but doesn't seem to be getting anything:

    // This one works:
    socket.on("start_test_broadcast", function() {
            console.log("I am a client and I heard a START_TEST command!");
            $('#overlay').fadeOut();
        });
   // This one also works:
   socket.on("end_test_broadcast", function() {
            console.log("I am a client and I heard an END command!");
            $('#overlay').fadeIn();
        });
// This one never arrives:
        socket.on("start_p1_broadcast", function() {
            console.log("I am a client and I heard a START_P1 command!");
            $('#overlay').fadeOut();
        });

I've never seen anything like changing names of messages and then the messages not arriving.

Does anyone have any insight? Thanks!



via Bar Code

No comments:

Post a Comment