Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to construct Socket object? #137

Closed
overflowz opened this issue Oct 11, 2016 · 14 comments
Closed

How to construct Socket object? #137

overflowz opened this issue Oct 11, 2016 · 14 comments

Comments

@overflowz
Copy link
Contributor

Hi, I'm trying to make a game and whenever I use a socket to do things, I'm saving every kind of information in key/value store. Problem is that, I can't save Socket object itself and I'm curious what's the correct way to construct Socket object? What information do I need?

Regards.

@darrachequesne
Copy link
Member

Hi! May I ask why you'd want to persist the socket object itself? Don't you mean the data attached to it instead?

@overflowz
Copy link
Contributor Author

overflowz commented Oct 12, 2016

Well, actually not. Imagine the following scenario:

  1. There's a lobby room, which waits for players to connect.
  2. Players get connected
  3. Lobby room checks if there's enough players
  4. Lobby creates new room and redirects e.g 3 connected players there and waits for another ones again.
  5. Adds more events on socket object <-- important part.

Because I can have only socket id at the moment, I don't know how to get actual Socket object from it to add more events at certain moments.

@lakamsani
Copy link

lakamsani commented Feb 3, 2017

Hi, I have a related use case. After making this call, I 'd like to send a message to one of the sockets after filtering the list on some socket attributes.
https://github.com/socketio/socket.io-redis#redisadapterclientsroomsarray-fnfunction

Without the socket.io redis adapter, it is possible to something like this:

func getSocketForEmail(email) {
    var ns = global.io.of(namespace || "/");
   for (var id in ns.connected) {
      var currentSocket = ns.connected[id]
      if (currentSocket.email.equals(email)) {
        return currentSocket
      }
  }
}

@overflowz
Copy link
Contributor Author

Actually it's not, if you're using clustered mode.
Let's say, socket A connects to 1st instance and socket B connects to 2nd instance.
You can't get socket A from 2nd instance.

@lakamsani
Copy link

lakamsani commented Feb 3, 2017

@overflowz right, that is what I 'm trying to confirm. The sample code I gave was without the cluster and the redis adapter (i.e a single node case). I don't really need the socket object, I just need the ability to query on socket attributes and send a message remotely to that socket (kind of like https://github.com/socketio/socket.io-emitter). I wonder if http://socketcluster.io/#!/ has a feature like this.

@overflowz
Copy link
Contributor Author

@lakamsani I've rewritten whole logic in my application and made it in a different way. Now you can use customRequest method, check the README page.

Regards.

@lakamsani
Copy link

@overflowz I noticed that but wasn't sure if it can be used to solve this use case. Based on your comment then, will something like this work to address my use case?

// on sender
io.adapter.customRequest('<some json>', function(err, replies){
  console.log(replies); // an array ['response1','response2', ...] with one element per node
});

// on each of the other nodes
io.adapter.customHook = function (data, cb) {
// if there is a local socket matching attributes in data param then send the message and ack via cb
  cb("I did it")

OR 

// no matching local socket.
  cb("I do not have that socket")
}

Is it possible to pass arbitrary JSON to the first parameter of io.adapter.customRequest? thanks.

@overflowz
Copy link
Contributor Author

@lakamsani I actually haven't tested that method by myself, but according to latest socket.io version, it should be io.sockets.adapter (instead of io.adapter) and yes, it should work fine (I think).

@lakamsani
Copy link

@overflowz this seems to work in our preliminary testing with 3.1.0 version of socket.io-redis. One issue we found is that the second arg of customHook i.e. 'cb' is undefined. We don't really need it for our use case. We are doing more testing but looks like it it will save from implementing this ourselves either using redis pub/sub directly or something like Amazon SQS.

@overflowz
Copy link
Contributor Author

@lakamsani that's pretty weird! Can you produce a snippet for that please? Thanks!

@darrachequesne
Copy link
Member

@lakamsani hi! I guess that's because #181 is not released yet.

@lakamsani
Copy link

@darrachequesne thanks for that info. We will look for it in the next release. @overflowz sample code is quite simple actually. Write something like as in the doc and try to invoke the callback..

io.sockets.adapter.customHook = function (data,cb) {
 cb("good"); // this will fail in 3.1.0 as cb is undefined 
}

@overflowz
Copy link
Contributor Author

@lakamsani yeah, that's because latest version was not released though. I was tired a lot when I replied it :)

@darrachequesne
Copy link
Member

Released as 4.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants