I have a setup with with the server listening to the clients, then sending them realtime notifications. The server code looks like this:
var io = require('socket.io').listen(8888);
io.sockets.on('connection', function (socket) {
socket.emit('connected', { connected: true });
console.log("New user " + socket.id);
socket.on('ready for data', function (data) {
pg_client.on('notification', function(content) {
socket.broadcast.to('ROOM').emit('update', content)
});
});
});
Whenever a notification comes in from the DB, all clients in ROOM get the message. This works, but if a client refreshes their page, they are now listening twice, and get two notifications for each update. socket.id's get added but never go away. Here is the client code:
var socket = require('socket.io-client')('http://IP:8888')
socket.on('connected', function (data) {
socket.emit('ready for data', {});
});
// Triggered by an addChannel function
socket.emit('add room', {room: 'ROOM'})
socket.on('update', function(data) {
// This gets hit multiple times per update, depending on the number of client refreshes
})
I've tried substituting once for on in both the client and the sever side with no luck. This causes only the first update to come through, and no others
via Bonnie Scott
No comments:
Post a Comment