Python - topic.subscribe()

Subscribes to a topic.

from nitric.resources import topic
from nitric.application import Nitric
from nitric.context import EventContext
updates = topic("updates")
@updates.subscribe
async def updates_sub(ctx: EventContext):
print(ctx.req.payload)
Nitric.run()

Parameters

  • Name
    middleware
    Required
    Required
    Type
    Middleware
    Description

    The middleware (code) to be triggered by the topic.

Examples

Subscribe to a topic

from nitric.resources import topic
from nitric.application import Nitric
from nitric.context import EventContext
updates = topic("updates")
@updates.subscribe
async def updates_sub(ctx: EventContext):
print(ctx.req.payload)
Nitric.run()

Notes

  • A function may only subscribe to a topic once, if multiple subscribers are required, create them in different services.
  • A function may subscribe to OR publish to a topic but not both. This is in place to stop services calling themselves infinitely.
Last updated on Oct 11, 2024