Guide#

Eric is a toolkit for async messaging: channels deliver messages to their subscribers, called connections, and stream them to clients in the channel’s output format. The format of the prefabs is the one of Server Sent Events, so a web server can return a channel’s stream as it is.

Everything works in memory out of the box. Persistence, durable queues, error handling and custom channels are optional parts you add when you need them.

A first example#

The following code creates a channel, subscribes to it, sends a message and streams it. Here is the complete example.

import asyncio

from eric_sse.message import Message
from eric_sse.prefabs import SSEChannel

channel = SSEChannel()
connection = channel.add_connection()
channel.dispatch(connection.id, Message(msg_type='welcome', msg_payload='Hello!'))

async def main():
    async for sse_event in channel.message_stream(connection.id):
        print(f'Received message: {sse_event}')

if __name__ == '__main__':
    asyncio.run(main())

The model#

How a message travels#

dispatch / broadcast ──> before_dispatch hook ──> connection's queue
                                                        │
                 client <── adapt <── on_message <── consumer
  1. dispatch or broadcast passes the message to the before_dispatch() hook, that can change it or drop it, then pushes it to the queue of each recipient connection. Sending never waits for consumers.

  2. A consumer pops the message from the queue, the connection’s listener receives it in on_message, and the channel adapts it to its output format.

There are three ways to consume a connection’s messages:

  • message_stream(): an async stream, what a web server returns to a client. It starts the listener, waits for messages without consuming CPU, and ends when the listener is stopped.

  • deliver_next() and wait_next(): one message at a time, synchronous or async, for code that pulls messages itself. They require a started listener.

  • process_queue(): runs the listener on every queued message in parallel, in an executor, see DataProcessingChannel.

Prefabs#

Making it yours#

Each part has its own extension point, so you only replace what you need:

To change

Do this

What happens when a message arrives

Subclass MessageQueueListener and override on_message()

The output format

Override adapt() and adapt_error(), or extend AbstractChannel

Channel behaviour: subscriptions, streams, filtering, errors

Override the channel’s hooks, e.g. on_connection_added(), on_stream_end(), before_dispatch(), on_error(). They do nothing by default

Where messages wait, e.g. a durable backend

Implement a Queue: only push() and pop() are required

Where channels and connections are stored

Implement a KvStorage and give it to a ChannelRepositoryBuilder, see Persistence

What happens when a queue or a listener fails

Register error handlers, see Error handling, or use the Dead Letter and Reliable Queue patterns

Persistence is always explicit: channels and their connections are stored when you call persist() on a repository, never implicitly. Messages are stored by queues themselves, so sending never needs a persist.

When you write your own storage, queue or channel, check it with the test suites in Verifying custom implementations. How to test a custom channel repository shows how, step by step.

Where to go next#