Skip to content

Multiple socket writes under io.configure #1062

@jhvst

Description

@jhvst

So I was struggling the past day with emitting data to a single client from my server-side. I genuinely gone trough every page of wiki and google result I could find with the keywords used and today I found out that it was all because I had my custom event inside my io.configure method.

My code looked following: (this did not work)

io.configure(function (){
  io.set('authorization', function (handshakeData, callback) {
    if (handshakeData.xdomain != false) {
    io.sockets.on('connection', function (socket) {
       socket.disconnect();
      });
    }
    callback(null, true); // error first callback style
  });

    io.sockets.on('connection', function (socket) {

       blabla

        socket.on('customevent', function () {

          io.sockets.socket(socket.id).emit('data', { dataexample: data });

        });

    });


});

But then again, this worked:


io.configure(function (){
  io.set('authorization', function (handshakeData, callback) {
    if (handshakeData.xdomain != false) {
    io.sockets.on('connection', function (socket) {
       socket.disconnect();
      });
    }
    callback(null, true); // error first callback style
  });

});

    io.sockets.on('connection', function (socket) {

     blabla

        socket.on('customevent', function () {

          io.sockets.socket(socket.id).emit('data', { dataexample: data });

        });


    });

Basically, the websocket ended up stacking the emitted data on every refresh, so that when one client emitted his private event, it triggered them all for every client.

Anyhow, I would be interested in accessing the handshakeData object inside my events, so I decided to open this issue.

I'm glad to demonstrate more when I get back from school.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions