//this Job sends an event that triggers the second job
client.defineJob({
  id: "job-1",
  name: "First job",
  version: "0.0.1",
  trigger: cronTrigger({
    cron: "0 9 * * *", // 9am every day (UTC)
  }),
  run: async (payload, io, ctx) => {
    //sends the "new.user" event with a userId in the payload
    await io.sendEvent("send-event", {
      name: "new.user",
      payload: {
        userId: "u_1234567890",
      },
    });
  },
});

client.defineJob({
  id: "job-2",
  name: "Second job",
  version: "0.0.1",
  //subscribes to the "new.user" event
  trigger: eventTrigger({
    name: "new.user",
    schema: z.object({
      userId: z.string(),
    }),
  }),
  run: async (payload, io, ctx) => {
    await io.logger.log("New user created", { userId: payload.userId });
    //do stuff with the new user
  },
});

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

If you want to send an event from outside a run (e.g. just from your backend) you should use client.sendEvent() instead.

Use eventTrigger() on a Job to listen for events.

For multiple events, use io.sendEvents() instead.

Parameters

cacheKey
string
required

Should be a stable and unique cache key inside the run(). See resumability for more information.

event
object
required
options
object

Returns

//this Job sends an event that triggers the second job
client.defineJob({
  id: "job-1",
  name: "First job",
  version: "0.0.1",
  trigger: cronTrigger({
    cron: "0 9 * * *", // 9am every day (UTC)
  }),
  run: async (payload, io, ctx) => {
    //sends the "new.user" event with a userId in the payload
    await io.sendEvent("send-event", {
      name: "new.user",
      payload: {
        userId: "u_1234567890",
      },
    });
  },
});

client.defineJob({
  id: "job-2",
  name: "Second job",
  version: "0.0.1",
  //subscribes to the "new.user" event
  trigger: eventTrigger({
    name: "new.user",
    schema: z.object({
      userId: z.string(),
    }),
  }),
  run: async (payload, io, ctx) => {
    await io.logger.log("New user created", { userId: payload.userId });
    //do stuff with the new user
  },
});