Links

Transacting on Messages

What is a transaction?

A transaction is an action that can be triggered when a message is received. A message can have multiple transactions. Each transaction has an input value, an output value and a unique transaction key (txKey) per message.
Using Buildable's SDK, you can set up:
  1. 1.
    Single transactions on a Message
  2. 2.
    Multiple transactions on a Message
  3. 3.
    Transactions on historical Messages

Single transactions on a Message

Messages from internal apps
const { createClient } = require('@buildable/messages');
const client = createClient(process.env.BUILDABLE_SECRET_KEY);
// Transact on `user.created`
client.on("user.created", async ({ event, payload }) => {
console.log("Receieved message with payload: ", payload);
// Any value returned here will be set as the transaction's output
});
Messages from third-party cloud apps
Simply add two additional options to the listener:
const { createClient } = require('@buildable/messages');
const client = createClient(process.env.BUILDABLE_SECRET_KEY);
// Listen on `customer.created` from Stripe
client.on("customer.created", async ({ event, payload }) => {
console.log("Receieved message with payload: ", payload);
}, {
platform: "stripe", // Platform name
label: "my-stripe-connection" // Connection name
});

Multiple transactions on a Message

In order to trigger multiple transactions on the same message, you must specify a custom transaction key (txKey).
For example, if two services need to listen on the same message and trigger different custom logic, each service must pass a unique txKey to the listener.
const { createClient } = require('@buildable/messages');
const client = createClient(process.env.BUILDABLE_SECRET_KEY);
// Transaction #1 on `user.created` from service A
client.on("user.created", async ({ event, payload }) => {
// Set the notification.sent transaction output
return await sendNotification(payload);
}, {
platform: "custom",
label: "demo", // Buildable secret key name
txKey: "notification.sent"
});
// Transaction #2 on `user.created` from service B
client.on("user.created", async ({ event, payload }) => {
// Set the salesforce.customer.created transaction output
return await createCustomerOnSalesForce(payload);
}, {
platform: "custom",
label: "demo", // Buildable secret key name
txKey: "salesforce.customer.created"
});
By default, the txKey is set to sdk.{{EVENT_NAME}}

Transacting on historical Messages

In order to transact on messages that have already been emitted, simply pass in the additional configuration argument since.
For example, to transact on messages from 10 days ago:
const { createClient } = require('@buildable/messages');
const client = createClient(process.env.BUILDABLE_SECRET_KEY);
client.on("user.created", async ({ event, payload }) => {
// Set the notification.sent transaction output
return await sendNotification(payload);
}, {
platform: "custom",
label: "demo", // Buildable secret key name
txKey: "notification.sent",
since: Date.now() - (10 * 24 * 60 * 60 * 1000) // Since 10 days ago
});
This feature is useful for creating new transactions on previously emitted messages.