Notification system with php and

I am trying to create a notification system with php and The idea is that clients connect to and wait for notification. The PHP script connects to 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('').listen(notification_port);
var clients = new Array();

io.sockets.on("connection", function(socket){
   socket.on("__identification", function(message){
      if ({
         console.log("user with session id " + + " connected!");
         var sockets = clients[];
         if (!sockets){
            sockets = new Array();
         clients[] = 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[];
  if (sockets){
     for (var i = 0; i < sockets.length; i++){
        sockets[i].emit("notification", query);
  } else {



source to share

2 answers

The problem was that the connection gets lost every minute. You must collect gargabe in the "disconnect" function and reinitialize the connection in the "connect" function



you need to add a disable handler as described below:

socket.on('disconnect', function () {
    //delete socket from sockets;




All Articles