Skip to content

Commit e4ce36b

Browse files
committed
support for 'payload' of notification in postgres >=9.0
1 parent 863a1ba commit e4ce36b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/binding.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static Persistent<String> routine_symbol;
3434
static Persistent<String> name_symbol;
3535
static Persistent<String> value_symbol;
3636
static Persistent<String> type_symbol;
37+
static Persistent<String> channel_symbol;
38+
static Persistent<String> payload_symbol;
3739

3840
class Connection : public EventEmitter {
3941

@@ -70,6 +72,8 @@ class Connection : public EventEmitter {
7072
name_symbol = NODE_PSYMBOL("name");
7173
value_symbol = NODE_PSYMBOL("value");
7274
type_symbol = NODE_PSYMBOL("type");
75+
channel_symbol = NODE_PSYMBOL("channel");
76+
payload_symbol = NODE_PSYMBOL("payload");
7377

7478

7579
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
@@ -390,7 +394,8 @@ class Connection : public EventEmitter {
390394
PGnotify *notify;
391395
while ((notify = PQnotifies(connection_))) {
392396
Local<Object> result = Object::New();
393-
result->Set(String::New("channel"), String::New(notify->relname));
397+
result->Set(channel_symbol, String::New(notify->relname));
398+
result->Set(payload_symbol, String::New(notify->extra));
394399
Handle<Value> res = (Handle<Value>)result;
395400
Emit((Handle<String>)String::New("notification"), 1, &res);
396401
PQfreemem(notify);

test/integration/client/notice-tests.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ test('emits notify message', function() {
1616
client.query('LISTEN boom', assert.calls(function() {
1717
var otherClient = helper.client();
1818
otherClient.query('LISTEN boom', assert.calls(function() {
19-
client.query('NOTIFY boom');
19+
client.query("NOTIFY boom, 'omg!'");
2020
assert.emits(client, 'notification', function(msg) {
21-
assert.equal(msg.channel, 'boom');
22-
client.end()
21+
22+
//make sure PQfreemem doesn't invalidate string pointers
23+
setTimeout(function() {
24+
assert.equal(msg.channel, 'boom');
25+
assert.ok(msg.payload == 'omg!' /*9.x*/ || msg.payload == '' /*8.x*/, "expected blank payload or correct payload but got " + msg.message)
26+
client.end()
27+
}, 500)
28+
2329
});
2430
assert.emits(otherClient, 'notification', function(msg) {
2531
assert.equal(msg.channel, 'boom');

0 commit comments

Comments
 (0)