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;
}
callback(message);
},
outgoing: function(message, callback) {
if (message.channel === '/meta/handshake') {
var parts = message.id.split(':');
var token = tokens[parts[1]];
delete tokens[parts[1]];
message.ext = message.ext || {};
message.ext.tokenResponse = someFunctionOf(token);
}
callback(message);
}
};