Node.js - topic.subscribe()
Subscribe a handler to a topic and receive new events for processing.
import { topic } from '@nitric/sdk'
const updates = topic('updates')
updates.subscribe(async (ctx) => {
// Log the provided message
console.log(ctx.req.json())
})
Parameters
- Name
middleware
- Required
- Required
- Type
- EventMiddleware or EventMiddleware[]
- Description
The middleware (code) to be triggered by the topic.
Examples
Subscribe to a topic
import { topic } from '@nitric/sdk'
const updates = topic('updates')
updates.subscribe(async (ctx) => {
// Log the provided message
console.log(ctx.req.json())
})
Notes
- A service may only subscribe to a topic once, if multiple subscribers are required, create them in different service.
- A service may subscribe to OR publish to a topic but not both