Subscribe to ERC20 Blockchain Events using public Docker Container
The goal of this docker image is to run a websocket locally that listens to samrt contract event ( ERC20 Transfer Event ), and polls the logs. It can be used to integrate with frontend where transaction data of a token is required.
-
Pull the docker image
docker pull devabhay28/blockchain-eventlistener
-
[ Important ] In the working directory create a .env file and paste your ws rpc url (get one from alchemy for free) make sure you're using same network for both RPC url and Contract deployment
Env Example:
RPC_URL=<ws-rpc-url> CONTRACT_ADD=<contract-address>
-
Run docker in same dir, here -dp take is used to map a local port (6000, localhost port where websocket will run) to docker port (3000, dont change this)
docker run -dp 6000:3000 --env-file .env devabhay28/blockchain-eventlistener
-
Now that the docker is running and websocket is ready. You can use packages such as
ws
to listen to transactions. Below is an example client code snippetconst WebSocket = require("ws"); const ws = new WebSocket("ws://localhost:6000"); ws.on("open", () => { console.log("Connected to WebSocket server"); }); ws.on("message", (message) => { const jsonString = message.toString(); const eventData = JSON.parse(jsonString); console.log("New transaction:", eventData); });