|
1
|
{"slug":"/references/event-service","title":"How to Use Event Module","frontmatter":{"slug":"/references/event-service","tags":["event","server","how to"],"sidebar_label":"Use Event Module","keywords":["event","provider","integration"]},"toc":[{"title":"Resolve Event Module's Service","id":"resolve-event-module-s-service","level":2},{"title":"emit","id":"emit","level":2},{"title":"Example","id":"example","level":3},{"title":"Type Parameters","id":"emit-type-parameters","level":3},{"title":"Parameters","id":"emit-parameters","level":3},{"title":"Returns","id":"emit-returns","level":3},{"title":"subscribe","id":"subscribe","level":2},{"title":"Example","id":"example-1","level":3},{"title":"Parameters","id":"subscribe-parameters","level":3},{"title":"Returns","id":"subscribe-returns","level":3},{"title":"unsubscribe","id":"unsubscribe","level":2},{"title":"Example","id":"example-2","level":3},{"title":"Parameters","id":"unsubscribe-parameters","level":3},{"title":"Returns","id":"unsubscribe-returns","level":3},{"title":"releaseGroupedEvents","id":"releasegroupedevents","level":2},{"title":"Example","id":"example-3","level":3},{"title":"Parameters","id":"releasegroupedevents-parameters","level":3},{"title":"Returns","id":"releasegroupedevents-returns","level":3},{"title":"clearGroupedEvents","id":"cleargroupedevents","level":2},{"title":"Example","id":"example-4","level":3},{"title":"Parameters","id":"cleargroupedevents-parameters","level":3},{"title":"Returns","id":"cleargroupedevents-returns","level":3},{"title":"addInterceptor","id":"addinterceptor","level":2},{"title":"Example","id":"example-5","level":3},{"title":"Parameters","id":"addinterceptor-parameters","level":3},{"title":"Returns","id":"addinterceptor-returns","level":3},{"title":"removeInterceptor","id":"removeinterceptor","level":2},{"title":"Example","id":"example-6","level":3},{"title":"Parameters","id":"removeinterceptor-parameters","level":3},{"title":"Returns","id":"removeinterceptor-returns","level":3}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn about the different methods in the Event Module's service and how to use them."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Resolve Event Module's Service","id":"resolve-event-module-s-service"},{"kind":"markdown","html":"In your workflow's step, you can resolve the Event Module's service from the Medusa container:\n\n```ts\nimport { Modules } from \"@medusajs/framework/utils\"\nimport { createStep } from \"@medusajs/framework/workflows-sdk\"\n\nconst step1 = createStep(\n \"step-1\",\n async ({}, { container }) => {\n const eventModuleService = container.resolve(\n Modules.EVENT_BUS\n )\n \n // TODO use eventModuleService\n } \n)\n```\n\nThis will resolve the service of the configured Event Module, which is the [Local Event Module](https://docs.medusajs.com/resources/infrastructure-modules/event/local) by default.\n\nYou can then use the Event Module's service's methods in the step. The rest of this guide details these methods.\n\n---"},{"kind":"heading","level":2,"text":"emit","id":"emit"},{"kind":"markdown","html":"This method emits one or more events. Subscribers listening to the event(s) are executed asynchronously."},{"kind":"heading","level":3,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nawait eventModuleService.emit({\n name: \"user.created\",\n data: {\n user_id: \"user_123\"\n }\n})\n```"},{"kind":"heading","level":3,"text":"Type Parameters","id":"emit-type-parameters"},{"kind":"typeList","sectionTitle":"emit","types":[{"name":"T","type":"`object`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Parameters","id":"emit-parameters"},{"kind":"typeList","sectionTitle":"emit","types":[{"name":"data","type":"[Message](/references/types/EventBusTypes/types/types.EventBusTypes.Message)<T> \\| [Message](/references/types/EventBusTypes/types/types.EventBusTypes.Message)<T>[]","description":"The details of the events to emit.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"name","type":"`string`","description":"The event's name.","optional":false,"defaultValue":"","example":"user.created","expandable":false,"children":[]},{"name":"metadata","type":"[EventMetadata](/references/types/EventBusTypes/types/types.EventBusTypes.EventMetadata)","description":"Additional meadata to pass with the event.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"eventGroupId","type":"`string`","description":"The ID of the event's group. Grouped events are useful when you have distributed transactions\nwhere you need to explicitly group, release and clear events upon lifecycle events of a transaction.\n\nWhen set, you must release the grouped events using the Event Module's `releaseGroupedEvents` method to emit the events.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"created_at","type":"`Date`","description":"The date the event was emitted.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"published_at","type":"`Date`","description":"The date the event was published to the event bus. If it's a grouped event, this will be the date the group was released.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"data","type":"TData","description":"The data payload that subscribers receive. For example, the ID or IDs of the created user. (e.g. { id: \"123\" } or { ids: [\"123\", \"456\"] })","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"options","type":"`Record<string, unknown>`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"options","type":"`Record<string, unknown>`","description":"Additional options for the event.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"emit-returns"},{"kind":"typeList","sectionTitle":"emit","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"This method emits one or more events. Subscribers listening to the event(s) are executed asynchronously.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"subscribe","id":"subscribe"},{"kind":"markdown","html":"This method adds a subscriber to an event. It's mainly used internally to register subscribers."},{"kind":"heading","level":3,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\neventModuleService.subscribe(\"user.created\", async (data) => {\n console.log(\"User created\", data)\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"subscribe-parameters"},{"kind":"typeList","sectionTitle":"subscribe","types":[{"name":"eventName","type":"`string` \\| `symbol`","description":"The name of the event to subscribe to.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subscriber","type":"[Subscriber](/references/types/EventBusTypes/types/types.EventBusTypes.Subscriber)","description":"The subscriber function to execute when the event is emitted.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[SubscriberContext](/references/types/EventBusTypes/types/types.EventBusTypes.SubscriberContext)","description":"The context of the subscriber.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"subscriberId","type":"`string`","description":"The ID of the subscriber. Useful when retrying failed subscribers.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"subscribe-returns"},{"kind":"typeList","sectionTitle":"subscribe","types":[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"The instance of the Event Module","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"unsubscribe","id":"unsubscribe"},{"kind":"markdown","html":"This method removes a subscriber from an event. It's mainly used internally to unregister subscribers."},{"kind":"heading","level":3,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\neventModuleService.unsubscribe(\"user.created\", async (data) => {\n console.log(\"User created\", data)\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"unsubscribe-parameters"},{"kind":"typeList","sectionTitle":"unsubscribe","types":[{"name":"eventName","type":"`string` \\| `symbol`","description":"The name of the event to unsubscribe from.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"subscriber","type":"[Subscriber](/references/types/EventBusTypes/types/types.EventBusTypes.Subscriber)","description":"The subscriber function to remove.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"context","type":"[SubscriberContext](/references/types/EventBusTypes/types/types.EventBusTypes.SubscriberContext)","description":"The context of the subscriber.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"subscriberId","type":"`string`","description":"The ID of the subscriber. Useful when retrying failed subscribers.","optional":false,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"unsubscribe-returns"},{"kind":"typeList","sectionTitle":"unsubscribe","types":[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"The instance of the Event Module","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"releaseGroupedEvents","id":"releasegroupedevents"},{"kind":"markdown","html":"This method emits all events in the specified group. Grouped events are useful when you have distributed transactions\nwhere you need to explicitly group, release and clear events upon lifecycle events of a transaction."},{"kind":"heading","level":3,"text":"Example","id":"example-3"},{"kind":"markdown","html":"```ts\nawait eventModuleService.releaseGroupedEvents(\"group_123\")\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"releasegroupedevents-parameters"},{"kind":"typeList","sectionTitle":"releaseGroupedEvents","types":[{"name":"eventGroupId","type":"`string`","description":"The ID of the event group.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"releasegroupedevents-returns"},{"kind":"typeList","sectionTitle":"releaseGroupedEvents","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"This method emits all events in the specified group. Grouped events are useful when you have distributed transactions\nwhere you need to explicitly group, release and clear events upon lifecycle events of a transaction.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"clearGroupedEvents","id":"cleargroupedevents"},{"kind":"markdown","html":"This method removes all events in the specified group. Grouped events are useful when you have distributed transactions\nwhere you need to explicitly group, release and clear events upon lifecycle events of a transaction."},{"kind":"heading","level":3,"text":"Example","id":"example-4"},{"kind":"markdown","html":"```ts\nawait eventModuleService.clearGroupedEvents(\"group_123\")\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"cleargroupedevents-parameters"},{"kind":"typeList","sectionTitle":"clearGroupedEvents","types":[{"name":"eventGroupId","type":"`string`","description":"The ID of the event group.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"options","type":"`object`","description":"Additional options for the event.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"eventNames","type":"`string`[]","description":"The names of the events to clear. If not provided, The group will\nbe entirely cleared.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"cleargroupedevents-returns"},{"kind":"typeList","sectionTitle":"clearGroupedEvents","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"This method removes all events in the specified group. Grouped events are useful when you have distributed transactions\nwhere you need to explicitly group, release and clear events upon lifecycle events of a transaction.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"addInterceptor","id":"addinterceptor"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method adds an interceptor to the event bus. This means that the interceptor will be\ncalled before the event is emitted."},{"kind":"heading","level":3,"text":"Example","id":"example-5"},{"kind":"markdown","html":"```ts\neventModuleService.addInterceptor((message, context) => {\n console.log(\"Interceptor\", message, context)\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"addinterceptor-parameters"},{"kind":"typeList","sectionTitle":"addInterceptor","types":[{"name":"interceptor","type":"[InterceptorSubscriber](/references/types/EventBusTypes/types/types.EventBusTypes.InterceptorSubscriber)","description":"The interceptor to add.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"addinterceptor-returns"},{"kind":"typeList","sectionTitle":"addInterceptor","types":[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"The instance of the Event Module","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"removeInterceptor","id":"removeinterceptor"},{"kind":"badges","badges":[{"variant":"neutral","label":"optional"}]},{"kind":"markdown","html":"This method removes an interceptor from the event bus."},{"kind":"heading","level":3,"text":"Example","id":"example-6"},{"kind":"markdown","html":"```ts\neventModuleService.removeInterceptor((message, context) => {\n console.log(\"Interceptor\", message, context)\n})\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"removeinterceptor-parameters"},{"kind":"typeList","sectionTitle":"removeInterceptor","types":[{"name":"interceptor","type":"[InterceptorSubscriber](/references/types/EventBusTypes/types/types.EventBusTypes.InterceptorSubscriber)","description":"The interceptor to remove.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"removeinterceptor-returns"},{"kind":"typeList","sectionTitle":"removeInterceptor","types":[{"name":"this","type":"`this`","optional":false,"defaultValue":"","description":"The instance of the Event Module","expandable":false,"children":[]}]}]} |