//this Job subscribes to an event called new.user
client.defineJob({
  id: "job-2",
  name: "Second job",
  version: "0.0.1",
  //subscribes to the "new.user" event
  trigger: eventTrigger({
    //sent events with this name will trigger this Job
    name: "new.user",
    //the expected payload shape
    schema: z.object({
      userId: z.string(),
      tier: z.union([z.literal("free"), z.literal("pro")]),
    }),
    //only events with the tier: "pro" will trigger this Job
    filter: {
      tier: ["pro"],
    },
    //(optional) example event object
    examples: [
      {
        id: "issue.opened",
        name: "Issue opened",
        payload: {
          userId: "1234",
          tier: "free",
        },
        //optional
        icon: "github",
      },
    ],
  }),
  run: async (payload, io, ctx) => {
    await io.logger.log("New pro user created", { userId: payload.userId });
    //do stuff
  },
});

These are the docs for Trigger.dev v2 which will be deprecated on January 31st, 2025. You probably want the v3 docs.

There are two way to send an event that will trigger eventTrigger():

  1. Use client.sendEvent() from anywhere in your codebase.
  2. Use io.sendEvent() from inside a Job's run() function.

You can have multiple Jobs that subscribe to the same event, they will all trigger when the event gets delivered.

Parameters

options
object
required
//this Job subscribes to an event called new.user
client.defineJob({
  id: "job-2",
  name: "Second job",
  version: "0.0.1",
  //subscribes to the "new.user" event
  trigger: eventTrigger({
    //sent events with this name will trigger this Job
    name: "new.user",
    //the expected payload shape
    schema: z.object({
      userId: z.string(),
      tier: z.union([z.literal("free"), z.literal("pro")]),
    }),
    //only events with the tier: "pro" will trigger this Job
    filter: {
      tier: ["pro"],
    },
    //(optional) example event object
    examples: [
      {
        id: "issue.opened",
        name: "Issue opened",
        payload: {
          userId: "1234",
          tier: "free",
        },
        //optional
        icon: "github",
      },
    ],
  }),
  run: async (payload, io, ctx) => {
    await io.logger.log("New pro user created", { userId: payload.userId });
    //do stuff
  },
});