For this small project we will use socket.io-client to connect to a BitPay insight server and get real-time notifications on new transactions and blocks. When a transaction includes a relevant Bitcoin address we will send out an email via Mandrill for notification.

Let’s start by connecting to the websocket, you can either use the public insight interface BitPay is maintaining or you could run your own.

The code above will subscribe to the ‘inv’ room which is the room all new tx/blocks are transmitted to.

Now we would like to add a listener to the socket to catch every new transaction and perform a check on it

The code above will go through each new transaction and check if the address you’ve chosen either sent or received coins and will run the sendEmail function. The senttx variable is used to avoid sending out two emails when the relevant address is in the input and the output fields (For example, if the change address is identical to the input address).

The sendEmail function looks like this

That’s all. An email alert will be sent to the defined email address whenever a transaction occurs with the relevant myAddress value.

Full code example

Alternatively you could also get alerts for large transactions instead of tracking a specific Bitcoin address