socket.io - push notification to client when Firebase have a new child doesn't work -
the code below listening new child added firebase push client using socket.io, problem socket.io doesn't fire emit, idea of how make send data client?
server:
firebaseref.endat().limittolast(1).on('child_added', function (childsnapshot, prevchildkey) { console.log('new gps point added ' + childsnapshot.val()); io.on('connection', function (socket) { console.log('new gps point added 2' + childsnapshot.val()); socket.volatile.emit('gpspoint', childsnapshot.val()); socket.on('my other event', function (data) { console.log(data); }); }); });
client: (inside angular control)
var socket = io.connect('http://localhost:8080'); socket.on('gpspoint', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); });
the socket connection should done outside firebase function:
var sockets=[]; io.on('connection', function (socket) { console.log('new socket: '+ socket); sockets.push(socket);});
inside firebase function should use sockets connected client :
firebaseref.endat().limittolast(1).on('child_added', function (childsnapshot, prevchildkey) { for(var i=0;i<sockets.length;++i) { console.log('socket'+sockets[i]); socket=sockets[i]; socket.volatile.emit('gpspoint', childsnapshot.val()); }});
Comments
Post a Comment