Building a dApp with Real-Time Transaction Notifications
Learn how to integrate on-chain notifications with your dApp using Alchemy Notify v2.
dApps on Ethereum have come a long way over the last several years, both in popularity and in complexity. Unfortunately, the user experience of most dApps is lacking compared to web2 apps.
One key piece that is missing is real-time notifications of events.
Users need to know immediately when their trades execute, when their transactions fail, when their auction bid has been accepted, or when a wallet of interest has aped into some new token.
Without these notifications, trades can be missed, critical actions are forgotten, and ultimately users might abandon your dApp.
Until Alchemy Notify, building these real-time notifications into your dApp has traditionally been complicated, time-consuming, and error-prone.
With Alchemy, sending real-time push notifications to your users for critical events such as dropped transactions, mined transactions, wallet activity, and even gas price changes is straightforward, easy, and reliable.
Learn about the differences between Alchemy Notify v1 and v2 in our announcement.
In this tutorial, we’ll look at an example of how, with just a few lines of code, your dApp can integrate the power of Alchemy Notify v2.

Overview

  1. 1.
    High level walkthrough of the example project
  2. 2.
    Build the project using Heroku
    1. 1.
      Clone Github Repo & Set Up Heroku
    2. 2.
    3. 3.
      Alchemy Notify API & Register Webhook Notifications
    4. 4.
      Insert Alchemy Webhook Id and Auth Key
    5. 5.
      Deploy Heroku App!
  3. 3.
    Build the project from scratch
    1. 1.
      Create Express Server
    2. 2.
    3. 3.
      Alchemy Notify API & Register Webhook Notifications
    4. 4.
      Insert Alchemy Webhook Id and Auth Key
    5. 5.
      Create dApp Frontend
    6. 6.
      Deploy!

Our Example

For our example, we’ll create a dApp that notifies users on a frontend interface, in real-time, when activity happens on specific Ethereum addresses.
Here's what we'll use for our architecture:
  • Express for our server
  • WebSockets to communicate between the client and server
  • Alchemy Notify to monitor an address and send a push notification when there is new activity
If you don't want to spend real (mainnet) Ethereum to try out this tutorial, you can follow the same process using the Rinkeby testnet and get Rinkeby ETH from a faucet, so you can send transactions to and from your target address for testing without paying for gas.
Our example dApp will perform two functions:
  1. 1.
    Register a user’s address for notifications
  2. 2.
    Send a notification to that user when they receive or send transactions.

The “register a user’s address for notifications” flow looks like this:

  1. 1.
    A user connects to their wallet through the dApp
  2. 2.
    The user clicks to register their wallet address to be monitored
  3. 3.
    The dApp sends an event through WebSockets to the server to register the address
  4. 4.
    The server calls the Alchemy API to register the address with Alchemy Notify

The “send a notification” flow looks like this:

  1. 1.
    A change happens to the registered address (e.g. they send or receive a transaction)
  2. 2.
    Alchemy Notify calls the server webhook with the change information
  3. 3.
    The server notifies the client through WebSockets a change has been made to the address
  4. 4.
    The website frontend displays the JSON response from the Websocket, with all the metadata
We’ll go through two versions of the tutorial:
  1. 1.
    Cloning the Github Repo using Heroku
  2. 2.
    Doing it all from scratch
Build with Heroku
Build from Scratch

1. Set up a GitHub Repo and Heroku

In this tutorial, you will utilize Heroku for hosting a server and website.
If you choose to use Heroku, be sure to follow all of the following steps.
If you want to use another provider, see “Build Project From Scratch”.
GitHub - alchemyplatform/Alchemy-Notify-Tutorial: Tutorial for integrating transaction activity notifications into your dApp.
GitHub

a) Make a clone of the existing Github Repository

1
git clone https://github.com/alchemyplatform/Alchemy-Notify-Tutorial
2
cd Alchemy-Notify-Tutorial
Copied!

b) Install Heroku-CLI and verify/install dependencies

  • Download Heroku-CLI based on your OS
  • After installation, open your terminal and run heroku login. Follow the commands that follow to login to your Heroku account. If you don't have an account, sign up for one.
  • Run node --version. You must have any version of Node greater than 10 installed. If you don’t have it or have an older version, install a more recent version of Node.
  • Run npm --version. npm is installed with Node, so check that it’s there. If you don’t have it, install a more recent version of Node.

c) Initiate Heroku

  • Run heroku create to create your Heroku app.
Take note of the info that pops up in the terminal, especially the URL that looks like this: http://xxxxxxxxx.herokuapp.com/ We'll be using it later.

2. Create a Webhook with the Alchemy Notify API

First, let’s look at how notifications with Alchemy work.
There are two ways to create and modify notifications:
  1. 1.
  2. 2.
    Through the Alchemy Notify API.
For our example, we’ll work with both! Let’s start by looking at the dashboard.
If you don’t already have one, you’ll first need to create an account on Alchemy. The free version will work fine for getting started.
Once you have an account, go to the dashboard and select “Notify” from the top menu.
Here you’ll see the different kinds of notifications you can set up:
  • Address Activity
  • Dropped Transactions
  • Mined Transaction
  • Gas Price
Alchemy Notify API dashboard where web3 developers can create new webhooks.
For our example, we’ll use the Address Activity notification.
You can easily swap out any of the others for your own use case.
In the dashboard you can create all of your notifications, add the addresses you want to monitor, and add the webhook URL that Alchemy should communicate with when a notification gets triggered.
Alchemy sends all the relevant details to this webhook. Your server needs to simply create the webhook, receive the call, and process the information as needed.

a) Create a notification by clicking “Create Webhook” within "Address Activity"

Notify API dashboard showing how to create a new webhook based on address activity.

b) Enter the webhook URL

Get the Heroku URL that was created from Step 1 and add /alchemyhook to the end.
The Webhook URL will be http://xxxxxxxxx.herokuapp.com/alchemyhook

c) Enter a test Ethereum address here for our setup.

To finish the setup, add a test address like this or copy the wallet address you use for development from your metamask wallet.
0xab5801a7d398351b8be11c439e05c5b3259aec9b
We’ll add the real one we want to monitor programmatically through the API in the next step.

d) Select an app from the dropdown menu

Make sure the app selected is on the Ethereum network you want to test on; if you're testing on Rinkeby, select an app configured to it.

e) Click “Create Webhook”

Alchemy address activity webhook interface with the chain, network, webhook URL, and Ethereum address filled in.

3. Insert the Alchemy Webhook ID and Auth Key

Find your webhook ID in the panel area of the webhook you created.
Where to find the webhook ID.
Find the auth token on the top right of the notify page on your dashboard.
Where to find the Auth Key for the Alchemy Notify API.
Open the server.js file and change lines 37 and 43 in server.js to reflect your particular Alchemy webhook ID and auth token.
server.js
1
// add an address to a notification in Alchemy
2
3
async function addAddress(new_address) {
4
console.log("adding address " + new_address);
5
const body = { webhook_id: <your alchemy webhook id>, addresses_to_add: [new_address], addresses_to_remove: [] };
6
try {
7
fetch('https://dashboard.alchemyapi.io/api/update-webhook-addresses', {
8
method: 'PATCH',
9
body: JSON.stringify(body),
10
headers: { 'Content-Type': 'application/json' },
11
headers: { 'X-Alchemy-Token': <your alchemy token> }
12
})
13
.then(res => res.json())
14
.then(json => console.log(json));
15
}
16
catch (err) {
17
console.error(err);
18
}
19
}
Copied!
Note: both values are to be plugged in server.js within double quotes like a string. So if your webhook ID is wh_qykepvarqjh9txk9 please replace <your alchemy webhook id> below with"wh_qykepvarqjh9txk9"

4. Deploy and Test

1
git add . // to add changes
2
git commit -m "added Alchemy keys" // to add a comment
3
git push heroku master // to push and deploy your heroku app
Copied!
That’s all it takes!
If you now go to your Heroku deployment URL (e.g. http://xxxxxxxxx.herokuapp.com/), your dApp looks like this:
Example Heroku deployment URL after the Heroku app was deployed.

5. Add an Address via API

Click “Enable Ethereum” to connect to MetaMask and select a wallet you want to connect to from the drop-down menu.
Enable Ethereum and connect to MetaMask.
And now click “Enable Notifications on this address” to register the address:
Confirmation message that the Ethereum wallet address was added to notifications.
Confirm the wallet address was added to the notification in the Alchemy Dashboard.
And now, with everything in place, you can test out your dApp!
If you face any issues in Step 5 or 6 and the behavior is not as expected, you can use the command heroku logs -t to view the heroku logs for debugging.

6. Test the notifications

With everything in place, send a small amount of testnet ETH to the address, and you will get a notification from the client with all the necessary details.
Note: to use the app, you must click “Enable Ethereum,” connect the desired Metamask address, and click “Enable Notifications”.
Then, the webhook’s messages will show up on your frontend.
Example Ethereum address notification displayed in a dApp.
Congratulations on your dApp deployment!
Feel free to edit your app, change its behavior, or make the frontend more spiffy!

Build project from scratch

In this tutorial, you will use a generalized setup for a server and website that will allow you to run an Alchemy Notify webhook.

1. Create an Express node.js server

The server will be a simple Express node.js server, and it will do three things:
  1. 1.
    Create a WebSocket server to communicate with the client
  2. 2.
    Interact with the Alchemy Notify API to set up/modify notifications
  3. 3.
    Create a webhook URL to receive real-time notifications from Alchemy
This code, with some customization for your specific on-premise server, AWS, or other hosting provider, can be run to power the tutorial. You will need to install express, path, socket.io, and node-fetch into your environment.
First, start the Express server and establish routes for the client (index.html) and the Alchemy webhook, which allows Alchemy to send real-time notifications to the server.

2. Create a server.js file and set up the webhook routes

The first step is to create a server.js file in your project folder and set up the appropriate routes for your webhook and web requests.
1
// server.js
2
// start the express server with the appropriate routes for our webhook and web requests
3
4
var app = express()
5
.use(express.static(path.join(__dirname, 'public')))
6
.use(express.json())
7
.post('/alchemyhook', (req, res) => { notificationReceived(req); res.status(200).end() })
8
.get('/*', (req, res) => res.sendFile('/index.html'))
9
.listen(PORT, () => console.log(`Listening on ${PORT}`))
Copied!
Note: with the above setup, the webhook URL you’ll need later is https://<yoururl.com>/alchemyhook

3. Emit notification to clients

If a notification is received by the webhook, you may want to do any necessary processing and then send it back to the client.
In this simple case, you'll just emit the notification to all clients using WebSockets.
1
// server.js
2
// notification received from Alchemy from the webhook. Let the clients know.
3
function notificationReceived(req) {
4
console.log("notification received!");
5
io.emit('notification', JSON.stringify(req.body));
6
}
Copied!

4. Start your WebSocket server

Now, start the WebSockets server to communicate with the client dApp.
In this example, you can use a library that sits on top of WebSockets, socket.io.
1
// server.js
2
// start the websocket server
3
const io = socketIO(app);
Copied!

5. Listen for client connections/calls on the WebSocket server

With the WebSockets connection established, listen for clients to connect and disconnect.
You can also listen for specific calls from our clients.
In this example, we want to listen for clients that want to add new Ethereum addresses to be monitored, using register address.
1
//server.js
2
// listen for client connections/calls on the WebSocket server
3
4
io.on('connection', (socket) => {
5
console.log('Client connected');
6
socket.on('disconnect', () => console.log('Client disconnected'));
7
socket.on('register address', (msg) => {
8
//send address to Alchemy to add to notification
9
addAddress(msg);
10
});
11
});
Copied!

6. Add Alchemy Notify API functionality

When you receive a client request to add an address, you can use the Alchemy API to register the address with your Alchemy Notification.
1
//server.js
2
// add an address to a notification in Alchemy
3
4
async function addAddress(new_address) {
5
console.log("adding address " + new_address);
6
const body = { webhook_id: <your alchemy webhook id>, addresses_to_add: [new_address], addresses_to_remove: [] };
7
try {
8
fetch('https://dashboard.alchemyapi.io/api/update-webhook-addresses', {
9
method: 'PATCH',
10
body: JSON.stringify(body),
11
headers: { 'Content-Type': 'application/json' },
12
headers: { 'X-Alchemy-Token': <your alchemy token> }
13
})
14
.then(res => res.json())
15
.then(json => console.log(json));
16
}
17
catch (err) {
18
console.error(err);
19
}
20
}
Copied!
Note: Be sure to replace <your alchemy token> with your auth token found on the Alchemy Dashboard. The webhook_id here is a unique ID provided by Alchemy when we create the webhook in the next step.
Anything we can do in the Alchemy Notify dashboard we can also do programmatically through the Alchemy Notify API. We can create new notifications, modify them, add and remove addresses, etc.
For example, here is a quick call to get a list of all our Alchemy webhooks:
1
// server.js
2
3
async function getWebhooks() {
4
try {
5
fetch('https://dashboard.alchemyapi.io/api/team-webhooks', {
6
method: 'GET',
7
headers: { 'Content-Type': 'application/json' },
8
headers: { 'X-Alchemy-Token': <your alchemy token> }
9
})
10
.then(res => res.json())
11
.then(json => console.log(json));
12
}
13
catch (err) {
14
console.error(err);
15
}
16
}
Copied!
Your server is ready! Here is the entire sample server.js we have created together:
1
const express = require('express')
2
const path = require('path')
3
const socketIO = require('socket.io');
4
const PORT = process.env.PORT || 5000
5
const fetch = require('node-fetch');
6
7
// start the express server with the appropriate routes for our webhook and web requests
8
var app = express()
9
.use(express.static(path.join(__dirname, 'public')))
10
.use(express.json())
11
.post('/alchemyhook', (req, res) => { notificationReceived(req); res.status(200).end() })
12
.get('/*', (req, res) => res.sendFile(path.join(__dirname + '/index.html')))
13
.listen(PORT, () => console.log(`Listening on ${PORT}`))
14
15
// start the websocket server
16
const io = socketIO(app);
17
18
// listen for client connections/calls on the WebSocket server
19
io.on('connection', (socket) => {
20
console.log('Client connected');
21
socket.on('disconnect', () => console.log('Client disconnected'));
22
socket.on('register address', (msg) => {
23
//send address to Alchemy to add to notification
24
addAddress(msg);
25
});
26
});
27
28
// notification received from Alchemy from the webhook. Let the clients know.
29
function notificationReceived(req) {
30
console.log("notification received!");
31
io.emit('notification', JSON.stringify(req.body));
32
}
33
34
// add an address to a notification in Alchemy
35
async function addAddress(new_address) {
36
console.log("adding address " + new_address);
37
const body = { webhook_id: <your alchemy webhook id>, addresses_to_add: [new_address], addresses_to_remove: [] };
38
try {
39
fetch('https://dashboard.alchemyapi.io/api/update-webhook-addresses', {
40
method: 'PATCH',
41
body: JSON.stringify(body),
42
headers: { 'Content-Type': 'application/json' },
43
headers: { 'X-Alchemy-Token': <your alchemy token> }
44
})
45
.then(res => res.json())
46
.then(json => console.log(json));
47
}
48
catch (err) {
49
console.error(err);
50
}
51
}
Copied!
Note: The webhook ID and the alchemy auth token comes from the Alchemy UI which we use to create our webhook in the next step!

7. Complete Steps 2 & 3 from the Heroku-Serviced Project

  • Create a new webhook
  • Create an alchemy webhook URL
  • Get your webhook ID
  • Get your Notify API Auth Key

8. Create the frontend for your dApp

Finally, we’ll build our dApp. Our example client is extremely simple. Of course, you’ll want to integrate this code into your dApp as appropriate.
Our dApp is a simple HTML/JavaScript page that does several things:
  • Connect to our WebSockets server to communicate with the server
  • Use web3 to connect to a browser wallet (such as MetaMask)
  • Send a wallet address to the server to be monitored
  • Receive notifications from the server when the server receives Alchemy notifications
Note: You need two JavaScript libraries for this code to work: socket.io and web3.

a) Connect to the WebSocket server and listen for incoming notifications

First, you'll establish your WebSocket connection with the client, and then, you can set up a listener to receive messages from the server (e.g. address notifications).
We’ll simply display the contents of the notification in this example, but you’d obviously want to do something more interesting:
1
<script>
2
// connect to WebSocket server and start listening for notifications
3
let socket = io();
4
let el;
5
socket.on('notification', (notificationBody) => {
6
console.log("got notification");
7
el = document.getElementById('server-notification');
8
el.innerHTML = 'Look what just happened!: ' + notificationBody;
9
});
10
</script>
Copied!

b) Emit new addresses to monitor to the WebSocket server

The second piece of interesting code happens when the user clicks to add their Ethereum address to the list of monitored addresses.
When the user clicks the “enable notifications on my address” button, the code emits an event to the WebSockets requesting to register a new address.
As we saw above, the server then receives this event and sends the new address to the Alchemy API to register in the Alchemy Notification, closing our loop.
1
enableNotificationsButton.addEventListener('click', function (e) {
2
e.preventDefault();
3
console.log("send address");
4
if (showAccount.innerHTML) {
5
socket.emit('register address', showAccount.innerHTML);
6
}
7
alert(showAccount.innerHTML + " added to notifications.")
8
});
Copied!
Here is the entire sample index.html client that we built:
1
<html>
2
3
<head>
4
<script src="/socket.io/socket.io.js"></script>
5
<script src="web3.js"></script>
6
<script>
7
// connect to WebSocket server and start listening for notifications
8
let socket = io();
9
let el;
10
socket.on('notification', (notificationBody) => {
11
console.log("got notification");
12
el = document.getElementById('server-notification');
13
el.innerHTML = 'Look what just happened!: ' + notificationBody;
14
});
15
</script>
16
</head>
17
18
<body>
19
<button class="enableEthereumButton">Enable Ethereum</button>
20
<h2>Account: <span class="showAccount"></span></h2>
21
<button class="enableNotificationsButton">Enable Notifications on this address</button>
22
<script>
23
const ethereumButton = document.querySelector('.enableEthereumButton');
24
const showAccount = document.querySelector('.showAccount');
25
const enableNotificationsButton = document.querySelector('.enableNotificationsButton');
26
// when clicked, send request to server to register the connected Ethereum address with Alchemy
27
enableNotificationsButton.addEventListener('click', function (e) {
28
e.preventDefault();
29
console.log("send address");
30
if (showAccount.innerHTML) {
31
socket.emit('register address', showAccount.innerHTML);
32
}
33
alert(showAccount.innerHTML+" added to notifications.")
34
});
35
// when clicked, connect to a web3 Ethereum wallet and get the active account
36
ethereumButton.addEventListener('click', () => {
37
getAccount();
38
});
39
async function getAccount() {
40
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
41
const account = accounts[0];
42
showAccount.innerHTML = account;
43
}
44
</script>
45
<p id="server-notification"></p>
46
</body>
47
48
</html>
Copied!
That’s all it takes! With a quick build and deploy, our dApp looks like this:
An example of a dApp with real-time transaction notifications.
Note: You can deploy the above code on your provider of choice like AWS, Digital Ocean or Heroku. For the purposes of this section, we've deployed to Heroku.

9. Add an address via API

Click “Enable Ethereum” to connect to MetaMask and select a wallet you want to connect to from the drop-down menu.
Connecting the dApp frontend to MetaMask and selecting a wallet.
And now click “Enable Notifications on this address” to register our address:
Confirmation message that notifications were enabled for the selected address.
Now, confirm the wallet address has been added to the notification in your Dashboard.
Alchemy Dashboard with the Ethereum address listed underneath a webhook that monitors address activity.

10. Test your notifications

Now, send a small amount of testnet ETH to your address, to get a notification from the client with all the necessary details.
Example notification that is received when ETH was sent to a monitored Ethereum address.
This is a simple example, but there are many ways you can expand on this to build a dApp that is real-time responsive for your users.
Congratulations on your dApp deployment!
Feel free to edit your app, change its behavior, or make the frontend more spiffy!
Fork 🍴, build 🏗️, and design 📝off this repo!
GitHub - alchemyplatform/Alchemy-Notify-Tutorial: Tutorial for integrating transaction activity notifications into your dApp.
GitHub
And that's it! You now know how to use Alchemy Notify v2 to add notifications to your dApp! If you enjoyed this tutorial for setting Alchemy Notify on your dApp, give us a tweet @AlchemyPlatform, or share questions/feedback with the authors @crypt0zeke and @ankg404.
Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs! Ready to start using Alchemy Notify? Create a free Alchemy account and start building!
Copy link