当Firebase有一个新的孩子没有工作时,向客户推送通知

时间:2016-01-16 18:07:50

标签: push-notification socket.io firebase

以下代码正在侦听添加到Firebase的新子项,然后使用socket.io将其推送到客户端,socket.io不会触发发出的问题,任何关于如何使其发送数据的想法对客户?

服务器:

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);
    });
}); });

客户:(内角控制)

    var socket = io.connect('http://localhost:8080');
    socket.on('GpsPoint', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });

1 个答案:

答案 0 :(得分:0)

套接字连接应在Firebase功能之外完成:

var sockets=[];
io.on('connection', function (socket) {
console.log('New socket: '+ socket);
sockets.push(socket);});  

在Firebase功能中,我们应该使用已连接到客户端的套接字:

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());
}});