node.js - Node-Redis psubscribe with custom messages -
i under impression using library:
https://github.com/mranney/node_redis
could this:
var redis = require('redis'); var pub = redis.createclient(); var sub = redis.createclient(); sub.psubscribe('channel'); sub.on('message channel', function(){}); pub.publish('channel','a message xyz');
the problem can't seem sub.on() receive specific custom message. entry point seems fired sub.on('message')
or sub.on('pmessage')
, how can sub.on('my custom message')
invoked? hope makes sense.
this how node redis client works. reserves message
, pmessage
emit events notifying of subscribe
, psubscribe
events respectively.
https://github.com/mranney/node_redis#publish--subscribe
but if want syntax have in code can define own message format , eventemitter
, maybe:
var events = require('events'); var redis = require('redis'); var pub = redis.createclient(); var sub = redis.createclient(); var ee = new events.eventemitter(); ee.on('aweomseevent', function() { console.log('party!'); }); sub.on('subscribe', function() { var message = { type: 'aweomseevent', isawesome: true }; pub.publish('mychannel', json.stringify(message)); }); sub.subscribe('mychannel'); sub.on('message', function(channel, message) { var deserialized = json.parse(message); if (deserialized.type) { ee.emit(deserialized.type, deserialized); } });