Event listener script in Next.js

1.6k views Asked by At

I want to use a node script that contains an events listener function (e.g. https://portal.thirdweb.com/sdk/advanced-features/contract-events#listen-to-all-events) inside my Next.js app.

As there is no server and the serverless functions need a trigger, I'm not sure where this kind of code should live.

How can I handle this case?

1

There are 1 answers

0
A Maj On

Any event listening should live on it's own server/not on serverless functions. The issue is that if you want to listen for events constantly and save them somewhere/act on it, the scripts that's listening for events needs to be constantly running.

With serverless functions (which is what all Next.js API endpoints are), they're only active when they get called, which won't work for event listening.

I would suggest building a simple Express server for event listening, and then deploying it somewhere like Zeet to have it always listening.