Skip to main content

Events

Scramjet facilitates the communication between Instances through an asynchronous way of handling and responding to events. Scramjet's Runner and Python Runner uses Node.js Event emitter and Python EventEmitter to propagate Event or Python Event messages between Scramjet's API and a Sequence. An event follows a publish-subscribe pattern, where the object emitting the event is known as the publisher, and the objects listening for the event are known as subscribers or listeners. When an event occurs, the listener will execute a callback function within an Instance or trigger further actions based on the event, whether it is a communication with other Instances or passing a payload to an event callback function.
Overall, Scramjet's Sequences are designed to be event-driven, communicating with each other if necessary, to enhance flexibility and extensibility. The below example will provide a brief illustration on how events are triggered. The two event methods that can be used in the Sequence are:

  • emit is used to trigger an event in an Instance. The emitted event can be accessed by using /instance/<Instance-id>/event API endpoint or through SDK InstanceClient.

  • on is used to add a callback function that will to be executed in an Instance when the event is received. This method can be executed by sending a named Event with the required payload to the Instance using /instance/<Instance-id>/_event API endpoint or through SDK InstanceClient.

import { InertApp } from "@scramjet/types";

const mod = async function(_stream) {
this.on("test-event", async (message) => {
this.emit("emitted-event", `reply to ${message}`)
});

await new Promise(res => setTimeout(res, 60000));
} as InertApp;

export default mod;

Was it helpful?

Didn't find information needed?

Join our Scramjet Community on Discord, where you can get help from our engineers directly.