Skip to content

Instantly share code, notes, and snippets.

@bootrino
Created August 29, 2018 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootrino/efd3d6adc291630472d13e1f42b24eed to your computer and use it in GitHub Desktop.
Save bootrino/efd3d6adc291630472d13e1f42b24eed to your computer and use it in GitHub Desktop.
untested code fragments for Postgres NOTIFY and LISTEN from nodejs
# untested code fragments for Postgres NOTIFY and LISTEN from nodejs
const {Pool, Client} = require('pg')
const dbUsername = 'dbusername'
const dbHost = '127.0.0.1'
const dbPort = '5432'
const dbPassword = 'supersecret'
const dbDatabasename = 'dbname'
const connectionString = `postgresql://${dbUsername}:${dbPassword}@${dbHost}:${dbPort}/${dbDatabasename}`
const pool = new Pool({connectionString})
const util = require('util')
import get from 'lodash.get'
pool.on('acquire', () => console.log("database: client is checked out from the pool"))
pool.on('connect', () => console.log("database: pool established new client connection to back end"))
pool.on('remove', () => console.log("database: client is closed & removed from the pool "))
pool.on('error', () => console.log("database: pool error"))
const config = {
application_name: "nodejs",
connectionString: connectionString, // e.g. postgres://user:password@host:5432/database
max: 20,
}
class postgresNotificationListener {
// we deliberately drop the connection when the listening ends, and reestablish when it starts again
// we cannot use the pool, must use a client for notifications because we need to hold the connection open
client: any;
connect = () => {
return new Promise(async (resolve: any, reject: any) => {
this.client = new Client(config);
await this.client.connect()
resolve()
})
}
stopListening = () => {
return new Promise(async (resolve: any, reject: any) => {
console.log('database: stopListening')
await this.client.removeAllListeners();
await this.client.end();
this.client = undefined;
resolve()
})
};
startListening = (channel: any, callback?: Function) => {
return new Promise(async (resolve: any, reject: any) => {
console.log('database: startListening')
if (!this.client) {
await this.connect();
}
this.client.query(`LISTEN ${channel}`)
this.client.on('notification', (notification: any) => {
console.log(notification.channel)
console.log("database: HEARD A MESSAGE: ", notification.payload)
if (!!callback) callback(decodeURIComponent(notification.payload));
resolve(decodeURIComponent(notification.payload));
})
})
};
}
const sendPostgresNotify = (channel: any, payload: any) => {
return new Promise(async (resolve: any, reject: any) => {
try {
console.log(`NOTIFY ${channel}, ${payload}`)
if (typeof payload !== "string") throw("notification payload must be a string")
await pool.query(`NOTIFY ${channel}, '${encodeURIComponent(payload)}'`)
resolve();
} catch (e) {
console.log(e.stack)
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment