βοΈ Writing Custom Producers & Consumers
Dispytch doesnβt lock you into any specific messaging backend. If you want to connect to something like Redis Streams, SQS, or whatever queue you wantβyou can do that by implementing your own Producer
and Consumer
.
Hereβs how.
π§ͺ Custom Producer
To build your own event emitter backend, implement the Producer
interface.
βοΈ Interface
1 2 3 4 |
|
π‘ Notes
topic
: where the event goespayload
: a dict containingid
,type
, andbody
of the eventconfig
: optional backend-specific config, usually declared in the event model as__backend_config__
- If your send logic times out raise
ProducerTimeout
β Example (Pseudocode)
1 2 3 4 5 6 7 |
|
π§ Custom Consumer
To receive and handle events from your own backend, implement the Consumer
interface.
βοΈ Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
π‘ Notes
-
listen()
must yieldEvent
objects. This is an async generator. -
ack()
is called when Dispytch successfully processes an event. Use it to mark the event as handled (e.g., ack a Kafka offset or delete a message from a queue).
β Example (Pseudocode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
π οΈ Use Your Custom Classes
Once implemented, you can use your custom producer and consumer classes directly in EventEmitter
and EventListener