Real-Time Notifications using pg_notify with Neon Postgres
A step-by-step guide describing how to implement real-time notifications using pg_notify in Postgres
This step-by-step guide shows how you can implement real-time notifications in Postgres (powered by Neon). Real-time notifications provide a way to instantly notify users in an application. With pg_notify and Postgres triggers, you can create a webhook-like system to invoke external services on specific database operations.
Prerequisites
To follow the steps in this guide, you will need the following:
- Node.js 18 or later
- A Neon account
Steps
- Provisioning a Postgres database powered by Neon
- Creating a new Node.js application
- Set up triggers
- Set up a Notifications Listener
- Notify using triggers
Provisioning a Postgres database powered by Neon
To get started, go to the Neon console and enter the name of your choice as the project name.
You will then be presented with a dialog that provides a connecting string of your database. Make sure to uncheck the Pooled connection checkbox on the top right of the dialog and the connecting string automatically updates in the box below it.
All Neon connection strings have the following format:
user
is the database user.password
is the database user’s password.endpoint_hostname
is the host with neon.tech as the TLD.port
is the Neon port number. The default port number is 5432.dbname
is the name of the database. “neondb” is the default database created with each Neon project.?sslmode=require
an optional query parameter that enforces the SSL mode while connecting to the Postgres instance for better security.
Save this connecting string somewhere safe to be used as the DATABASE_URL
further in the guide. Proceed further in this guide to create a Node.js application.
Creating a new Node.js application
To start building the application, create a new Node.js project. Open your terminal and run the following command:
Further, execute the following command to install the dependencies to read the environment variables and connect to Postgres:
The libraries installed include:
pg
: A Postgres client for Node.js.dotenv
: A library for handling environment variables.
Now, let's move on to setting up event triggers that will send notifications upon insertion of a row in a specific table.
Set up triggers
To set up event triggers for a specific table (say my_table
), you will define a trigger function called my_trigger_function
. Create a file named setup.js
with the following code:
In the code above, my_trigger_function
function uses pg_notify
to send a notification to the channel named my_channel
with the content of the newly inserted message
. Finally, it creates a trigger named my_trigger
that will execute this function after each insertion in table named my_table
. This ensures that any new message inserted into the table triggers the notification.
To execute the script as above, run the following command:
Now, let's move to setting up a notifications listener in Node.js.
Set up a Notifications Listener
To listen for notifications in Node.js intended for the channel named my_channel
, create a file listen.js
with the following code:
The code above begins with importing pg
and loading all the enviroment variables into scope. Further, it initializes a client connection to your Postgres. In the listenToNotifications
function, it sets up a listener to notifications using client.on('notification', ...)
callback. To invoke the callback, it starts listening for notifications to channel named my_channel
, using LISTEN my_channel
command.
To keep listening to the notifications, you would want to keep running the following command:
Now, let's insert a row to invoke the triggers that will notify the listeners.
Notify using triggers
To notify the listeners, you will use Postgres triggers. To programtically trigger an event that will be created upon insertion into the table named my_table
, create a file send.js
with the following code:
The code above begins with importing pg
and loading all the enviroment variables into scope. Further, it initializes a client connection to your Postgres. In the insertRow
function, it simply inserts a row into the table named my_table
.
To execute the script as above, run the following command:
Note
As by default, Neon's Autosuspend scales to zero after 5 minutes of inactivity, the NOTIFY/LISTEN command(s) only exist for the duration of the current session, and are lost when the session ends.
Summary
In this guide, you learned how to receive and send real-time notifications using pg_notify
in Serverless Postgres. Using Postgres triggers, you can selectively listen to changes happening in specific database table(s), and perform a function that invokes pg_notify
to send out the notifications to the connected listeners.
Source code
You can find the source code for the application described in this guide on GitHub.
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.
Last updated on