Accessing Content in XMPP PubSub Event Using pubsub:published Event (StanzaJS)

22 Views Asked by At

I'm currently working on handling XMPP PubSub events in my application using the 'pubsub:published' event. However, I'm facing an issue when trying to access the content of the published message.

pub.sub.helpers.ts

/* 
 *   Called when a subscriber recieves a new item
*/
export function listenForPublishEvents(ag: XMPP.Agent) {

    ag.on('pubsub:published', async (msg,) => {

        const jid = ag.config.jid as string
        // console.log(`${jid} recieved ${msg.pubsub.items}`)
        console.log(msg.pubsub.items.published);

        const { node, published } = msg.pubsub.items


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

        // Throws item not found for unknown reason
        return data
    })
}

The issue is that the published array in msg.pubsub.items only contains an object with an id property, and the actual content is not directly present.

How can I modify my code to correctly access the content of the published message? Am I missing something in the XMPP PubSub configuration, or is there a different approach to retrieve the content?

pub.sub.plugin.ts

// References: https://github.com/legastero/stanza/blob/master/docs/Using_PubSub.md
export const NS_MY_PUBSUB = environment.pubSubService
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 function plugin(client: Agent, stanzas: JXT.Registry) {
    stanzas.define({
        // 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
    })
}```




Tried looking into documentation and examples from github repo.
0

There are 0 best solutions below