Usage
Publish & Subscribe
Subscriber: Also known as the “consumer” or “listener”, the subscriber able to subscribe to particular topic and receives events from it. When the publisher sends out a message, all relevant subscribers get notified.
Here we’ll deliver an event to multiple consumers.
// subscriber.js
async function onEcho({payload}) {
console.log('new message', payload)
}
async function main() {
// join code here
let subscription = await wamps.subscribe(
'net.example',
{},
onEcho,
)
console.log(`new subscription ID=${subscription.ID} URI=${subscription.URI}`)
}
Publisher: Also known as the “producer” or “event emitter”, the publisher is responsible for sending events to topic. It can broadcast to specifically target or to particular subscribers.
// publisher.js
async function main() {
# join code here
await wamps.publish('net.example', 'Hello, WAMP!')
}
Output:
new message: Hello, WAMP!