Pratik Makune
Pratik Makune

Reputation: 31

How to make Sails.js app listen to events of ethereum blokchain to perform CRUD on persistence layer

I have a smart contract deployed on Ethereum blockchain and it emits some event with necessary data.

I have a sails.js application which needs to listen to this event.

Roughly, the JavaScript code looks like:

var event = contract.myEvent();
event.watch((err, res) => {
    console.log(res); // event response
    // API call to DB for persistence
});

Where should this code sit in sails.js application to work correct?

Upvotes: 0

Views: 122

Answers (1)

maroodb
maroodb

Reputation: 1118

This code should be excuted as a Service at the application start time.

for example you can create a file named EventsService.js :

let event = contract.myEvent();

exports.start = function () {

  event.watch((err, res) => {
    console.log(res); // event response
    // API call to DB for persistence
});
}

and then you can start the service like this: (from the app.js file)

const eventService = require('path/to/EventService.js');

eventService.start();

Upvotes: 0

Related Questions