sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.09k stars 126 forks source link

EventBus needs additional filters #533

Closed mckamyk closed 5 days ago

mckamyk commented 2 weeks ago

I know EventBus for aws isn't quite ready for production yet but just wanted to note this behavior.

I've been playing with getting them working with the existing versions and they do work, but I've noticed there doesn't seem to be any filters applied.

If I have this Event.

import {event} from 'sst/event'
import {ZodValidator} from 'sst/event/validator'

const buildEvent = event.builder({
  validator: ZodValidator
})

export const FooEvent = buildEvent("app.FooEvent", z.object({ foo: z.string() }))

export const BarEvent = buildEvent("app.BarEvent", z.object({ bar: z.string() }))

create handlers with

import {bus} from "sst/aws/bus"
import {FooEvent} from "./events"

export const handler = bus.subscriber(FooEvent, async properties => {
  console.log(properties.foo)
})

And in sst.config.ts...

// sst.config.ts
const bus = new sst.aws.Bus("MyBus")

bus.subscribe({
  handler: 'path/to/foo.handler',
})
bus.subscribe({
  handler: 'path/to/bar.handler',
})

If I then invoke FooEvent by doing...

// some kind of resource with bus linked
import {bus} from "sst/aws/bus"
import {FooEvent} from "./events"

bus.publish(Resource.MyBus, FooEvent, { foo: "whaa" })

I noticed both the foo and bar subscribers will get invoked.

To fix this I added the following to my sst.config.ts

bus.subscribe({
  handler: 'path/to/foo.handler'
}, {
  pattern: {
    detailType: ["app.FooEvent"],
    source: [`${$app.name}.${$app.stage}`] // for posterity ig
  }
})

bus.subscribe({
  handler: 'path/to/bar.handler'
}, {
  pattern: {
    detailType: ["app.BarEvent"],
    source: [`${$app.name}.${$app.stage}`]
  }
})
mckamyk commented 1 week ago

Shamelessly hijacking your attention, sst dev bun dev starts my dev server, but it appears sst dev does not pass in the AWS_ACCESS_KEY_ID and such needed.

running sst dev and sst shell bun dev works, as it appears sst shell ... does load these secrets as env vars.

jayair commented 6 days ago

@mckamyk Hmm maybe open a new issue for that?

mckamyk commented 6 days ago

598

thdxr commented 5 days ago

i'm not sure if this is a bug? ion eventbus will default to subscribing to all events (you can do a switch on the event)

if you do want to break it up into more detailed subscriptions you need to specify the filters as your example

mckamyk commented 5 days ago

I guess the reason I brought it up was because V2 EventBus had an extra argument in it.

  bus.subscribe("fooEvent", {  // I assumed "fooEvent" lead into pattern creation
    handler: "path/to/foo.handler",
    bind: [db],
  });

Whereas in Ion, this appears to be more manual.

Fine by me, as long as the docs reflect it.