0

I am using Faye with Node.js (javascript) for a chat server and I am trying to implement 'notices' so that when one use subscribes, the server will send a message to the channel with a property __messageType = 'subscribe'

The server code looks like so (the ext field is present and working just fine)

var chat = new Faye.NodeAdapter()... //etc
chat.addExtension({
    incoming: function(message, callback) {
        if (message.channel === '/meta/subscribe') {
            console.log('A new user subscribed');
            chat.getClient().publish(message.subscription, {
                ext: message.ext,
                __messageType: 'subscription'});
        }
        callback(message);
    }
});

On my client side I have attached an 'incoming' extension so that people can easily determine if this is a subscription notification and not an actual 'data message'

clientChat.addExtension({
    incoming: function(message, callback) {
        console.log('Incoming message', message);
        message.getType = function() {
            return message.__messageType;
        };
        messge.getData = function(key) {
            return message.ext[key];
        };
        callback(message);
    }
});

I am structuring my messages this way that way people can do something like this:

var sub = new Faye.Client(url).subscribe('/messages', function(message) {
    if (message.getType() === 'subscribe') console.log('Someone subscribed');
    if (message.getType() === 'unsubscribe') console.log('Someone left');
    else console.log('Ext data: ', message.ext);

The problem is that my messages are coming through with the ext field wrapped in a 'data' field and I have no idea where it's coming from. My getType and getData methods have been successfully added, but they obviously don't work because the ext field is no longer present at the top level, they are instead embedded in the message's data field.

Expected:                                  Actual:
channel: "/messages"                       channel: "/messages/"
ext: { variousData }                       data: {ext: variousData}
getData: function (key) {                  getData: function(key) {
getType: function () {                     getType: function() {
id: "4"                                    id: "4"

Does anyone have any idea why I'm getting that 'data' field in my messages?

  • That's how the object you're sending is encoded into the message. The message itself includes the channel name and ID, and your object is passed along as the `data` attribute. However, you can easily encode your `type` attribute and check them using `message.data.messageType` – drnugent Apr 10 '14 at 23:01
  • Ahh, thank you. I just wasn't aware if this was the 'official' field that my message was coming in as or if something was getting mangled. Now I know I can always pass along message.data. –  Apr 11 '14 at 13:50

0 Answers0