Publish-subscribe support for @xmpp/client
.
npm install @xmpp-plugins/pubsub
import { client, xml } from "@xmpp/client"
import setupPubSub from "@xmpp-plugins/pubsub"
const PUBSUB_SERVICE_ID = 'pubsub.example.com'
const PUBSUB_NODE = 'chatter'
const xmpp = client({service: 'wss://xmpp.example.com'})
const pubSubPlugin = setupPubSub(xmpp)
pubSubPlugin.on(`item-published:${PUBSUB_SERVICE_ID}`, ev => {
console.log('An item was published on ${PUBSUB_SERVICE_ID}', ev)
})
await pubSubPlugin.createNode(PUBSUB_SERVICE_ID, PUBSUB_NODE)
// Publish a single entry with the string "Hello, world"
await pubSubPlugin.publish(PUBSUB_SERVICE_ID, PUBSUB_NODE,
xml('item', {}, xml('entry', {}, 'Hello, world!')))
const nodeId = await pubSubPlugin.createNode('service.example.com', 'nodeName')
console.log(`nodeName pubsub node created with id ${nodeId}`)
await pubSubPlugin.deleteNode('service.example.com', 'nodeName')
You will need to create a <item>
XML item and provide that to the publish
method.
const itemId = await pubSubPlugin.publish('service.example.com', 'nodeName',
xml('item', {}, 'Hello, world!'))
console.log(`Published with id ${itemId}`)
await pubSubPlugin.retract('service.example.com', 'nodeName', 'bnd81g37d61f49fgn581')
const item = await pubSubPlugin.get('service.example.com', 'nodeName', 'bnd81g37d61f49fgn581')
console.log(item ? `Found your item: ${item}` : 'The item does not exist')
A basic call will return the last published items.
const { rsm, items } = await pubSubPlugin.items('service.example.com', 'nodeName')
console.log('Found some items', items)
You can also pass in an object with result set management
const { rsm, items } = await pubSubPlugin.items('service.example.com', 'nodeName', { max: 500 })
console.log('Found some items', items)
event | description |
---|---|
last-item-published:<service> |
Optionally sent automatically after subscription with information on the last published item |
last-item-published:<service>:<node> |
Optionally sent automatically after subscription with information on the last published item |
item-published:<service> |
Sent when a new item is published to any pub-sub node on the service |
item-published:<service>:<node> |
Sent when a new item is published to the given node |
item-deleted:<service> |
Sent when a new item is deleted from any pub-sub node on the service |
item-deleted:<service>:<node> |
Sent when a new item is deleted from the given node |