PubSub
@klotho::pubsub
Specified on a Node.js event emitter to enable asynchronous message passing between more publisher and subscriber execution units in the cloud.
Directives
Directive | Type | Description |
---|---|---|
id | string | (Required) The identifier of the underlying PubSub resource that this pubsub declaration is bound to. |
Supported operations
Operation | Notes |
---|---|
on | Fully supported |
emit | Fully supported |
caution
Attempting to invoke any unsupported methods or access properties on an EventEmitter
annotated with @klotho::pubsub
will fail at run-time in the Klotho-compiled version of your application.
Examples
- Javascript
- Typescript
Declaring a New PubSub Capability
const events = require("events");
/** @klotho::pubsub {
* id = "my-pubsub"
* }
*/
const pubsubEmitter = new events.EventEmitter();
Subscribing to an Event
pubsubEmitter.on("my-event", (e) => {
console.log(`handling my-event: ${JSON.stringify(e)}`);
});
Publishing an Event
pubsubEmitter.emit("my-event", myEvent);
Declaring a New PubSub Capability
import * as events from "events";
/** @klotho::pubsub {
* id = "my-pubsub"
* }
*/
const pubsubEmitter = new events.EventEmitter();
Subscribing to an Event
pubsubEmitter.on("my-event", (e) => {
console.log(`handling my-event: ${JSON.stringify(e)}`);
});
Publishing an Event
pubsubEmitter.emit("my-event", myEvent);