Python - topic.subscribe()

Subscribes to a topic.

from nitric.resources import topic
from nitric.application import Nitric
from nitric.context import MessageContext
updates = topic("updates")
@updates.subscribe
async def updates_sub(ctx: MessageContext):
print(ctx.req.data)
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 MessageContext
updates = topic("updates")
@updates.subscribe
async def updates_sub(ctx: MessageContext):
print(ctx.req.data)
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 22, 2024