Notification system with php and socket.io
I am trying to create a notification system with php and socket.io. The idea is that clients connect to socket.io and wait for notification. The PHP script connects to socket.io via curl on a different port and even publishes an update that is sent to the connected clients. Clients are identified through the ID that they send in the message after the connection event. I keep the socket variable associated with user_id. Everything works fine, but after a while the script stops working. It looks like after a while the socket variable is stored in an array. However my server-side code is posted below
var notification_port = 8001;
var oak_port = 8002;
var io = require('socket.io').listen(notification_port);
var clients = new Array();
io.sockets.on("connection", function(socket){
socket.on("__identification", function(message){
if (message.id){
console.log("user with session id " + message.id + " connected!");
var sockets = clients[message.id];
if (!sockets){
sockets = new Array();
}
sockets.push(socket);
clients[message.id] = sockets;
}
});
});
var url = require('url');
var oakListener = require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var sockets = clients[query.id];
if (sockets){
for (var i = 0; i < sockets.length; i++){
sockets[i].emit("notification", query);
}
res.end('ok');
} else {
res.end('failed');
}
}).listen(oak_port);
+3
source to share