Skip to content

Sticky attributes ​

Sticky attributes are like attributes, except that they're copied to every message sent or published while the message is handled, and from those to the next, all the way down the flow. This page shows how to send and read them.

Use them for context that the whole flow needs, such as a tenant id or the user that started it. Set them with stickyAttributes:

ts
await bus.send(new ChargeCreditCard('tok_visa', 1200), {
  stickyAttributes: {
    tenantId: 'acme',
    requestedBy: 'user-42'
  }
})

Handlers receive them in the stickyAttributes of their second argument. Type them with the second type parameter of MessageAttributes:

ts
type TenantAttributes = MessageAttributes<
  {},
  { tenantId: string; requestedBy: string }
>

export const chargeCreditCardHandler = handlerFor<
  ChargeCreditCard,
  TenantAttributes
>(ChargeCreditCard, async (command, { stickyAttributes }, ctx) => {
  console.log('Charging card', { tenantId: stickyAttributes.tenantId })
  // CreditCardCharged carries the same sticky attributes
  await ctx.publish(
    new CreditCardCharged(command.creditCardToken, command.amount, new Date())
  )
})

Workflows use a sticky attribute to route replies back to the workflow that sent a command.

See also ​

Released under the MIT License.