Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time

Using PubSub

1. Create stanza definitions for our pubsub content

There are several existing payload formats that you can use, notably json, tune, avatar, and geoloc.

To create your own, the first step is to make a plugin for JXT to register the new stanza content:

// mycontentplugin.ts

import { Agent, jxt } from 'stanza';

export const NS_MY_PUBSUB = '';

export interface MyPubSubContent {
    // The itemType field is what is used to distinguish pubsub
    // item content types. It MUST be present when exporting,
    // but we're going to mark it as optional to be easier to
    // work with.
    itemType?: typeof NS_MY_PUBSUB;
    value: string;

export default function(client: Agent, stanzas: jxt.Registry) {
        // Inject our definition into all pubsub item content slots.
        // These slots are already registered with `itemType` as the
        // type field.
        aliases: jxt.pubsubItemContentAliases(),
        element: 'stuff',
        fields: {
            value: jxt.text()
        namespace: NS_MY_PUBSUB,
        // Specify the `itemType` value for our content.
        type: NS_MY_PUBSUB

We can then load the plugin with:

import MyContentPlugin from 'mycontentplugin';


2. Publishing our new content

client.publish('', 'ournode', {
    itemType: NS_MY_PUBSUB, // Again, `itemType` is required
    value: 'Away it goes!'

3. Subscribing to our content

client.subscribeToNode('', 'ournode');

If you want to subscribe using your bare JID, you can use:

client.subscribeToNode('', {
    node: 'ournode',
    useBareJID: true

4. Receiving publish events

    client.on('pubsub:published', msg => {
        const { node, published } = msg.pubsub.items;

        // Only process publishes for the node we care about
        if (node !== 'ournode') {

        // We can check that we got the expected data type
        // by checking the itemType:
        const itemType = published[0]!.content.itemType;
        if (itemType !== NS_MY_PUBSUB) {

        // Extract our content. Unfortunately, we have to assert the type here.
        const data = published[0]!.content as MyPubSubContent;

        // ...process the pubsub data

5. Unsubscribing

client.unsubscribeFromNode('', 'ournode');

Or, if you subscribed with a bare JID:

client.unsubscribeFromNode('', {
    node: 'ournode',
    useBareJID: true