Modify (on server) outgoing service message depending on incoming one

15 views
Skip to first unread message

eugene57

unread,
Mar 10, 2012, 5:06:42 AM3/10/12
to Faye users
Hello!

I'd like to set message.ext.token on client (for '/meta/handshake'
message) and somehow receive from server some response to this
particular token.
So, is it possible to add extra data to outgoing (from server) service
message (e.g. successful handshake-responce) depending on the
corresponding incoming message (e.g. handshake-request)? Or should i
try another approach?

James Coglan

unread,
Mar 12, 2012, 12:47:09 PM3/12/12
to faye-...@googlegroups.com
For most messages this is possible, because of the following properties of Bayeux:

* If an incoming message has an id, the same id is echoed back
* If an incoming message has a clientId, the same clientId is echoed back
* Message IDs are unique per-client

Because of this, most messages are uniquely identifiable by their combined id and clientId. But /meta/handshake has no incoming clientId, and the id is likely to be "1" -- it's the first message the client sends.

However, because we know the server will echo the id, we can do this:

* Pick a new random key for each incoming message
* Append this key to the message's id
* Store the token from the message against this key
* Let the server process the message
* Extract the original id and the key from the outgoing message
* Restore the message's real ID
* Look up the token using the key
* Attach some function of the token to the outgoing message

In code this would look something like this:

var tokens = {};

var extension = {
  incoming: function(message, callback) {
    if (message.channel === '/meta/handshake') {
      var key = pickARandomString();
      tokens[key] = message.ext.token;
      message.id = message.id + ':' + key;
    }
    callback(message);
  },
  
  outgoing: function(message, callback) {
    if (message.channel === '/meta/handshake') {
      var parts = message.id.split(':');
      message.id = parts[0];
      var token = tokens[parts[1]];
      delete tokens[parts[1]];
      message.ext = message.ext || {};
      message.ext.tokenResponse = someFunctionOf(token);
    }
    callback(message);
  }
};
Reply all
Reply to author
Forward
0 new messages