Martin
Martin

Reputation: 490

Socket.IO chat - "ping" specified client

I have simple Socket.IO based chat, and when User1 clicked on User2 at "friends list", User1 will be connected into room with User2 (based on "token" from server). Everything works without problem, when both have opened "chat window" (they are both in room).

But how can I "ping" the User2, when he have chat window closed (he isn't connected into room)?

Thank you for your help!

EDIT:

  1. User1 opened his "Friends list" and select User2 (chat window opened - like FB)
  2. Client JS request token for conversation User1 + User2
  3. Node.JS side connect User1 into room with name = token

Okey, now is User1 in room. But User2 is not, because he is not opened window (but he is "online" = connected into server io.connect(...)).

  1. User1 write some message and send it into room
  2. Now I need to "ping" User2's client side JS and open chat window (get token, connect into room) with new message

Simplified client JS:

socket = io.connect "...", {port: 1234}
token = ajaxRequestToServerForToken mineID, hisID
socket.emit "joinIntoRoom", token

Simplified node.js code:

//In handler class
joinIntoRoom: (socket, token) ->
    socket.leave socket.room
    socket.join token

    socket.emit "connected"

Upvotes: 0

Views: 1431

Answers (1)

Ed_
Ed_

Reputation: 19138

Ok, you need a way identify user2 so you can emit an event to him.

This question has a similar answer - does it help?

Once you have a socket object for user2, you can just call socket.emit('joinIntoRoom', {token: token, message: message}) with the first message, then the client should interpret that.

Upvotes: 0

Related Questions