Subscribe
Use step.subscribe() to open an inbox inside a run: every matching event that arrives becomes a message, kept in order. For a walkthrough, see step.subscribe.
step.subscribe() is experimental, and the API may change.
step.subscribe(id, sources): Inbox
- Name
id- Type
- string
- Required
- required
- Description
The ID of the step. Opening the inbox is one step.
- Name
sources- Type
- InboxSources
- Required
- required
- Description
One source or an array of them. A source is an event name, an
eventType(), or an object:Properties- Name
event- Type
- string | EventType
- Required
- required
- Description
The event to receive.
- Name
if- Type
- string
- Required
- optional
- Description
An expression the event must match. The arriving event is
asyncand the event that started the run isevent, as instep.waitForEvent(). See writing expressions.
// One source
await using inbox = step.subscribe("reviews", prReviewed);
// Several, filtered to this run's pull request
await using activity = step.subscribe("pr-activity", [
{ event: prReviewed, if: "async.data.pr == event.data.pr" },
{ event: prChecked, if: "async.data.pr == event.data.pr" },
]);
step.subscribe() returns at once. The inbox receives only events that arrive after it opens, though an event up to 5 seconds older can still match. await using (TypeScript 5.2 or later) closes it at the end of the block.
Inbox
Each inbox has its own sources. For a separate stream, open another inbox with step.subscribe().
A message is the event as it was sent, with name, data, id, ts and v. It's typed from the inbox's event types. A source given as a name has no schema, so its data is unknown.
With several sources, narrow a message with message.is(type), which takes one event type or an array. eventType.is(message) works too.
Import the types with import type { Inbox, InboxMessage } from "inngest". InboxMessage<typeof inbox> is the type of an inbox's messages, for code that handles them outside the loop:
const kindOf = (message: InboxMessage<typeof activity>) => {
return message.is(prReviewed) ? "review" : "check";
};
next(options?)
Returns the next unread message, waiting for one if none is unread.
- Name
timeout- Type
- number | string | Date
- Required
- optional
- Description
How long to wait: milliseconds, a duration such as
"30m", or aDate. Returnsnullif no message arrives in time.
Returns undefined once the inbox is closed, even for a read that was already waiting. Two reads return messages in order. A read that loses a race keeps its place for the next message.
nextBatch(options?)
Returns an array of every unread message, waiting for at least one. Each batch read is one step.
- Name
max- Type
- number
- Required
- optional
- Description
Takes at most this many messages, oldest first. The rest stays unread.
- Name
timeout- Type
- number | string | Date
- Required
- optional
- Description
Returns
nullif no message arrives in time.
Returns undefined once the inbox is closed.
drain(options?)
Returns every unread message without waiting, or []. Takes max, like nextBatch(). Each drain() is one step.
batches(options?)
Reads batch after batch with for await, using nextBatch(). Takes max and timeout. The loop ends when the inbox closes or a wait times out.
for await (const batch of inbox.batches({ max: 20 })) {
await step.run("handle-batch", () => handleBatch(batch));
}
[Symbol.asyncIterator]()
Reads one message at a time with for await, using next(). The loop ends when the inbox closes.
for await (const message of inbox) {
await step.run("handle", () => handle(message));
}
close()
Stops the inbox's sources and returns an array of the messages it hadn't read. close() is one step.
After close(), next() and nextBatch() return undefined, loops end, and drain() returns []. The executor takes the cut-off when it handles the close, so a message saved before then is returned if you hadn't read it, and a message that arrives after is dropped.
closed
true once the inbox is closed.