Skip to content

Instantly share code, notes, and snippets.

@dax
Created September 29, 2017 10:39
Show Gist options
  • Save dax/d7e98fb549acf51ab5741c1500efa183 to your computer and use it in GitHub Desktop.
Save dax/d7e98fb549acf51ab5741c1500efa183 to your computer and use it in GitHub Desktop.
PostgreSQL notify/listen demo
// npm install pg
// bootstrap : psql -h localhost test test
// create table config(id serial, conf json);
// insert into config (conf) values ('{"test": 42}'::json);
var pg = require('pg');
var Pool = pg.Pool;
var pool = new Pool({
user: 'test',
host: 'localhost',
database: 'test',
password: 'test',
port: 5432,
})
pool.connect()
.then(function(client) {
client.query('LISTEN test_event')
client.on('notification', function(message) {
console.log('Got event=', message)
})
return client.query({text: 'select * from config'})
.then(function(res) {
console.log('Got res=', res.rows[0]);
client.query("NOTIFY test_event, '" + JSON.stringify(res.rows[0].conf) + "'")
})
.catch(function(error) {
console.log('Got error=', error);
})
})
.catch(function(error) {
console.log('Pool error=', error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment