Extending Stanza.io messages with custom fields and attributes

Stanza.io is a library for using modern XMPP in the browser, and it does that by exposing everything as JSON. One of the promises they made that you have no need to ever see or touch any XML when using it.

I wanted a extended message type and got stuck until I found how awesome their plugin system is.

Here is how a plugin looks like:

function customMessagePlugin(client, stanzas) {  
    const NS = 'xmpp:happened';
    const messageAttribute = stanzas.define({
        name: 'happened',
        element: 'happened',
        namespace: NS,
        fields: {
            event: stanzas.utils.textSub(NS, 'event'),
            data: stanzas.utils.textSub(NS, 'data'),
        }
    });

    stanzas.withMessage(function (Message) {
        stanzas.extend(Message, messageAttribute);
    });
}

Using this plugin

client.use(customMessagePlugin);  

Sending message with custom attributes:

client.sendMessage({  
   to: someone@server.tld'
   happend: {
        event: 'some even',
        data: 'Foo data, data foo.'
     }
});

We can take this further by making our own method for sending message.

function customMessagePlugin(client, stanzas) {  
    const NS = 'xmpp:happened';
    const messageAttribute = stanzas.define({
        name: 'happened',
        element: 'happened',
        namespace: NS,
        fields: {
            event: stanzas.utils.textSub(NS, 'event'),
            data: stanzas.utils.textSub(NS, 'data'),
        }
    });

    stanzas.withMessage(function (Message) {
        stanzas.extend(Message, messageAttribute);
    });

     client.sendCustomEvent = function(message) {
         client.sendMessage({
            to: message.to
            happend: {
                event: message.event,
               data: message.data
            }
      });
     }
}

Sending message is now:

client.sendCustomEvent({  
        to: 'somejid@domin.tld'
        event: 'some even',
        data: 'Foo data, data foo.'
});

We can take it to another step further by setting incoming event for this type of message!

function customMessagePlugin(client, stanzas) {  
    const NS = 'xmpp:happened';
    const messageAttribute = stanzas.define({
        name: 'happened',
        element: 'happened',
        namespace: NS,
        fields: {
            event: stanzas.utils.textSub(NS, 'event'),
            data: stanzas.utils.textSub(NS, 'data'),
        }
    });

    stanzas.withMessage(function (Message) {
        stanzas.extend(Message, messageAttribute);
    });

     client.sendCustomEvent = function(event, data, to) {
         client.sendMessage({
            to: to
            happend: {
                event: event,
               data: data
            }
      });
     } 
     client.on('message', function(message) {
         if(message.happend) { 
         client.emit('customEventIncoming', meesage)
      }
     });
}

And listen for incoming customEventIncoming event!

client.on('customEventIncoming', function(customEvent) {  
     // do things
});

Stanza.io is very extensible, flexible and awesome library!

If you have any question feel free to leave a comment bellow.

Stanza.io plugin reference

Sabbir Ahmed

Coder, *nixer, Music lover

Dhaka, Bangladesh http://thesabbir.com/

Subscribe to console.log('blog')

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!