Created
September 29, 2017 10:39
-
-
Save dax/d7e98fb549acf51ab5741c1500efa183 to your computer and use it in GitHub Desktop.
PostgreSQL notify/listen demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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