A subscription method used to subscribe a general handler that must accept ALL messages that the system can receive. This method allows you to provide routing keys with wildcards, but it can't offer the same type-safety as the subscribe method. Handlers passed to this method must be ready to receive ANY message.
is an array of routing keys indicating which messages should arrive in this queue. (This
enforces that the messages be on the same exchange that was indicated on initialization.) For example, if you
initialized the class with the foo
exchange and you want to subscribe to all messages published to that exchange,
you would pass ['#']
here. If you want to subscribe to all messages starting with bar
and baz
, you might
pass ['bar.#', 'baz.#']
.
is the name of the queue that will be created for this subscription.
is the function that will be called when the message is received. It will be passed the message itself, the message's attributes, and a logger tagged with the messages id.
Optional
queueOpts: Omit<{ are the options for the subscription. This allows you to set a few queue properties if you'd like
type Thing1Msg = { key: 'my-domain.did.thing1'; data: { foo: string } };
type Thing2Msg = { key: 'my-domain.did.thing2'; data: { bar: number } };
type Thing3Msg = { key: 'my-domain.did.thing3'; data: { baz: boolean } };
// All the messages that we might receive
type AllMessages = Thing1Msg | Thing2Msg | Thing3Msg;
// A handler that handles two of these messages
const handlerForAnyMessage: MessageHandler<AllMessages> = async (msg, attrs, log) => {
// Do something...
switch (msg.key) {
case 'my-domain.did.thing1': {
// ...
}
// ...
}
return true;
}
export const subscribe = (deps: { amqp: WeenieSubscriberInterface<AllMessages> }) => {
// This subscription will receive all messages that start with 'my-domain.'
await deps.amqp.subscribeAny(['my-domain.*'], 'my-queue', handlerForAnyMessage);
}
A subscription method used to subscribe a handler for one or more specfic messages with a key or array of keys derived directly from that type.
This is used to enforce type-safety in the (common) case in which you are subscribing a specific handler to a specific routing key.
See also subscribeAny
is the name of the queue that will be created for this subscription.
is the function that will be called when the message is received. It will be passed the message itself, the message's attributes, and a logger tagged with the messages id.
Optional
queueOpts: Omit<{ are the options for the subscription. This allows you to set a few queue properties if you'd like
type Thing1Msg = { key: 'my-domain.did.thing1'; data: { foo: string } };
type Thing2Msg = { key: 'my-domain.did.thing2'; data: { bar: number } };
type Thing3Msg = { key: 'my-domain.did.thing3'; data: { baz: boolean } };
// A handler that handles two of these messages
const handlerForSomeMessages: MessageHandler<Thing1Msg | Thing2Msg> = async (msg, attrs, log) => {
// Do something...
switch (msg.key) {
case 'my-domain.did.thing1': {
// ...
}
// ...
}
return true;
}
export const subscribe = (deps: { amqp: WeenieSubscriberInterface<AllMessages> }) => {
await deps.amqp.subscribe(
['my-domain.did.thing1', 'my-domain.did.thing2'],
'my-queue',
handlerForSomeMessages,
);
}
Generated using TypeDoc
The subscriber side of a weenie pubsub handler