Sunday, 15 September 2013

node.js - Node + Redis + PHP getting RealTime Issue -



node.js - Node + Redis + PHP getting RealTime Issue -

i'm building pub/sub server in nodejs + redis + php, after dig in on subject , starting building came across confusion saw on process.

example scenario:

5 users connected socket through nodejscorrectly. one of them click button send custom alert connected users except him php receive the message alert trough phone call ajax (for purpose of example) php publish redis redis receive event , trigger socket.io broadcast event

set view:

<input type="text" id="message"> <button id="sendmessage">send alert</button>

client side js

var socket = io.connect('http://127.0.0.1:3000/'); socket.on('notification', function (data) { alert(data.message); console.log('message received'); }); // ajax event $(document).on('click','#sendmessage', function(){ $.ajax({ url: '/send/notification', method: 'post', type: 'json', data: { message: $('#message').val() }, success:function(data) { console.log('message sent'); } }) });

php server side communicate redis

$message = $_post['message']; $redis = new predis\client(); $redis->publish('notification',$message);

nodejs

at point on nodejs server go hear message event in redis broadcast event sockets here first issue met.

var http = require('http'); var app = http.createserver(); var io = require('socket.io')(app); var redis = require('redis'); app.listen(3000,'127.0.0.1',function(){ console.log("listening:", app.address().port, app.address().address); }); io.on('connection', function(client){ var redisclient = redis.createclient(); redisclient.subscribe('notification'); redisclient.on("message",function(channel,data){ // line should broadcast client except sender socket.broadcast.emit(channel,json.parse(data)); console.log(channel); }); }); issue

at point console.log() channel can see on terminal 5 logs "notification" why 5?

when socket.io broadcast event, 5 times, sending in case 5 alert() clients , 4 sender instead of 1 clients 0 sender.

the times depending on how many users connected don't understand missed, because shouldn't right behaviour.

i tried set creation of redisclient , subscription of channel outside of io.on('connection') no luck, same result.

another test

if broadcast event on connection of socket so:

io.on('connection', function(client) { client.broadcast.emit('notification','hello'); });

it works correctly, think redis problem. suggest appreciated.

you having 5 clients connected, "connection" event fired 5 times. there 5 listeners redis broadcast.

there 2 ways correctly:

a) 1 listener per connection, send message connection

io.on('connection', function(socket){ var redisclient = redis.createclient(); redisclient.subscribe('notification'); redisclient.on("message",function(channel,data){ // line should broadcast current connection socket.emit(channel,json.parse(data)); console.log(channel); }); });

b) 1 global listener, broadcast clients

var redisclient = redis.createclient(); redisclient.subscribe('notification'); redisclient.on("message",function(channel,data){ // line should broadcast client except sender io.sockets.emit(channel,json.parse(data)); console.log(channel); });

i prefer method b) needs 1 redis connection.

node.js sockets redis socket.io node-redis

No comments:

Post a Comment