Skip to content

More than one subscription, routing it to a specific handler #261

@skinkie

Description

@skinkie

The example denotes the following.

async with Client("test.mosquitto.org") as client:
    async with client.messages() as messages:
        await client.subscribe("humidity/#")
        async for message in messages:
            print(message.payload)

If a user wants to also test for temperature:

async with Client("test.mosquitto.org") as client:
    async with client.messages() as messages:
        await client.subscribe("humidity/#")
        await client.subscribe("temperature/#")
        async for message in messages:
            if message.topic.startswith('humidity'):
                print(message.payload)
            elif message.topic.startswith('temperature'):
                print(message.payload)

Is there any way to do this more elegantly? Maybe even in a concurrent way?

Pseudo:

async with Client("test.mosquitto.org") as client:
    async with client.messages() as messages:
        await client.subscribe("humidity/#")
        async for message in messages:
            print(message.payload)

    async with client.messages() as messages:
        await client.subscribe("temperature/#")
        async for message in messages:
            print(message.payload)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions