Documentation
Feedback
Guides
App Development

App Development
Services
Handling events
Receiving Catalog Changes on VTEX IO

Learn how to detect and handle catalog changes in your VTEX IO app.

This guide will teach you how to set up a system for detecting and responding to changes in the VTEX Catalog using VTEX IO.

We will use the Broadcaster app to trigger events and develop a new app to handle these events.

Before you begin

Ensure you are in a development workspace and install the Broadcaster app using the following command:


_10
vtex install vtex.broadcaster

The Broadcaster app broadcasts catalog changes to other parts of the VTEX system. In this guide, we will use it to listen for catalog modifications and execute tasks accordingly.

Cloning the boilerplate repository

Clone the events-example boilerplate by running the command presented on the right. This will create a local copy of the project, equipping you with the necessary files to start implementing the catalog change handling system.


_10
git clone https://github.com/vtex-apps/events-example

Setting the event handler

Navigate to the node/service.json file and configure the event handler's name to skuChange and the keys property to ["broadcaster.notification"].

The events field defines the events the service will listen to and specifies how it will handle them. In this case, the skuChange function will run whenever the event, identified by the key value, is triggered - specifically when the Broadcaster app triggers the notification event.

  • skuChange: Custom name given to the event handler function. You are free to choose a meaningful name for your events; in this case, it represents a change related to a Stock Keeping Unit (SKU).
  • keys: Property that specifies the event identifier the service will listen for. In this case, ["broadcaster.notification"].
node/service.json

_19
{
_19
"memory": 128,
_19
"ttl": 10,
_19
"timeout": 2,
_19
"minReplicas": 2,
_19
"maxReplicas": 10,
_19
"workers": 4,
_19
"events": {
_19
"skuChange": {
_19
"keys": ["broadcaster.notification"]
_19
}
_19
},
_19
"routes": {
_19
"hcheck": {
_19
"path": "/_v/app/events-example/hcheck",
_19
"public": true
_19
}
_19
}
_19
}

Developing the event handler function

After saving the node/service.json, edit the node/index.ts file to create the skuChange handler function.

node/service.json
node/index.ts

_47
import type {
_47
IOClients,
_47
ParamsContext,
_47
ServiceContext,
_47
RecorderState,
_47
} from '@vtex/api'
_47
import { Service } from '@vtex/api'
_47
import { setCacheContext } from './utils/cachedContext'
_47
_47
const TREE_SECONDS_MS = 3 * 1000
_47
const CONCURRENCY = 10
_47
_47
declare global {
_47
type Context = ServiceContext<IOClients, State>
_47
_47
interface State extends RecorderState {
_47
code: number
_47
}
_47
}
_47
_47
export default new Service<IOClients, State, ParamsContext>({
_47
clients: {
_47
options: {
_47
events: {
_47
exponentialTimeoutCoefficient: 2,
_47
exponentialBackoffCoefficient: 2,
_47
initialBackoffDelay: 50,
_47
retries: 1,
_47
timeout: TREE_SECONDS_MS,
_47
concurrency: CONCURRENCY,
_47
},
_47
},
_47
},
_47
events: {
_47
skuChange: (ctx: any) => {
_47
console.log('Received SKU changed event')
_47
console.log(ctx.body)
_47
},
_47
},
_47
routes: {
_47
hcheck: (ctx: any) => {
_47
setCacheContext(ctx)
_47
ctx.status = 200
_47
ctx.body = 'ok'
_47
},
_47
},
_47
})

Note that the skuChange function will log the Received SKU changed event message whenever the event-example app listens to an event from the broadcaster app.

For more complex functions, consider developing your event handler function in a separate file.

Linking the app

In the events-example, run vtex link and trigger an event to the Broadcaster app by making a POST request to app.io.vtex.com/vtex.broadcaster-adapter/v0/{{account}}/{{workspace}}/notify.

node/service.json
node/index.ts
Request

_10
curl --request post \
_10
--url https://app.io.vtex.com/vtex.broadcaster/v0/{account}/{workspace}/notify \
_10
--header 'Accept: application/json' \
_10
--header 'Content-Type: application/json' \
_10
--header 'X-VTEX-API-AppKey: {AppKey}' \
_10
--header 'X-VTEX-API-AppToken: {AppToken}' \
_10
--data '{"HasStockKeepingUnitModified": "true","IdSku":"1"}'

Response

_10
OK

Checking the handler function behavior

After linking the events-example app and firing the event with the Broadcast app, check the console for a log message similar to the following: Received SKU changed event service-node@6.38.3.

Now that your system is configured to detect catalog changes, it is time to tailor your code to respond effectively to these events according to your specific requirements. Customize your code to implement actions and functionalities that align with your desired outcomes in response to catalog modifications.

node/service.json
node/index.ts
Request

_10
13:29:52.531 - info: App running service-node@6.38.3
_10
13:29:58.810 - info: Received SKU changed event service-node@6.38.3
_10
13:29:58.815 - info: { HasStockKeepingUnitModified: true, IdSku: '1' } service-node@6.38.3
_10
13:29:58.824 - info: [16:29:59.186Z] [26] mystore/myworkspace:skuChange 204 POST /mystore/myworkspace/_events 2 ms service-node@6.38.3

Cloning the boilerplate repository

Clone the events-example boilerplate by running the command presented on the right. This will create a local copy of the project, equipping you with the necessary files to start implementing the catalog change handling system.

Setting the event handler

Navigate to the node/service.json file and configure the event handler's name to skuChange and the keys property to ["broadcaster.notification"].

The events field defines the events the service will listen to and specifies how it will handle them. In this case, the skuChange function will run whenever the event, identified by the key value, is triggered - specifically when the Broadcaster app triggers the notification event.

  • skuChange: Custom name given to the event handler function. You are free to choose a meaningful name for your events; in this case, it represents a change related to a Stock Keeping Unit (SKU).
  • keys: Property that specifies the event identifier the service will listen for. In this case, ["broadcaster.notification"].

Developing the event handler function

After saving the node/service.json, edit the node/index.ts file to create the skuChange handler function.

Note that the skuChange function will log the Received SKU changed event message whenever the event-example app listens to an event from the broadcaster app.

For more complex functions, consider developing your event handler function in a separate file.

Linking the app

In the events-example, run vtex link and trigger an event to the Broadcaster app by making a POST request to app.io.vtex.com/vtex.broadcaster-adapter/v0/{{account}}/{{workspace}}/notify.

Checking the handler function behavior

After linking the events-example app and firing the event with the Broadcast app, check the console for a log message similar to the following: Received SKU changed event service-node@6.38.3.

Now that your system is configured to detect catalog changes, it is time to tailor your code to respond effectively to these events according to your specific requirements. Customize your code to implement actions and functionalities that align with your desired outcomes in response to catalog modifications.


_10
git clone https://github.com/vtex-apps/events-example

Contributors
3
Photo of the contributor
Photo of the contributor
Photo of the contributor
+ 3 contributors
Was this helpful?
Yes
No
Suggest edits (Github)
Contributors
3
Photo of the contributor
Photo of the contributor
Photo of the contributor
+ 3 contributors
On this page