2

I am currently playing with Faye.js. Upon subscription to a channel I want that specific client to receive an object that would be irrelevant to anybody already in the channel.

How can this be achieved?

More detail: The object is an array of the last 20 chat comments in the room. Anybody already in the room would have received this object already or been a part of the chats and so it is not required for these to receive it.

Thank you in advance.

Julio
  • 6,182
  • 11
  • 53
  • 65

1 Answers1

1

The only way I've come across is to generate a client-side GUID. When the client connects, it announces itself to the others through a 'public' channel, you can then use the GUID to send messages directly to this client.

For example, take the piece of code from the answer of this previous question to generate something that looks like a UUID.

You can then do something on the client-side like this:

var guid = guidGenerator();
client.subscribe('/privChannel_' + guid, onPrivateMessage);
client.subscribe('/pubChannel', onPublicMessage);
client.publish('/announce', { 'myId': guid });

function onPrivateMessage() {
 // do something
};

function onPublicMessage() {
 // do something
};

Your server should always subscribe to the '/announce' channel, and when any message is posted in that channel it should store that id so that it can identify that particular client. Then, the server can use this id to publish to a channel only this client should be subscribed to.

Note however that this is not a good idea for sensitive data. Other clients could also subscribe to '/announce' and farm the guids for malicious purposes.

Community
  • 1
  • 1
Frank Razenberg
  • 511
  • 4
  • 17