2

I have a PHP application where people post different status messages. I want to implement realtime notifications in it.

That is if a user posts something, immediately other users accessing the site will get a prompt on their screen about a new post. I have heard you can use node.js to implement it. But I don't know how exactly it can be done. Any help will kindly be appreciated.

Morgan
  • 19,934
  • 8
  • 58
  • 84
Sanks
  • 141
  • 1
  • 3
  • 12
  • Questions are expected to show some code examples and minimal understanding of the problem. That being said it can be hard when you have no idea where to begin, but this question may get closed because the style is not a good fit for the site. Good luck with your quest for knowledge. – Morgan Jul 26 '13 at 19:41

2 Answers2

3

I suggest you to take a look at socket.io. Client send a message to node, node can save to db (or forward with a RESTful call to your php application) and notify all clients.

It's not php but only nodejs and js. Enjoy ;)

Example

SERVER:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

CLIENT

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

You have to write what you want to be done in the client in on("yourEvent", function() { ...

Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32
  • I am using the same technique in my app but now braining how can I get the session id of client? – Airy Mar 02 '14 at 04:59
  • @AbdulJabbarWebBestow: are you looking for PHP-SESSIONID? If yes, I don't know how to get. Socket.io has its own sessionId, refer to this SO answer: http://stackoverflow.com/questions/6979992/how-to-get-session-id-of-socket-io-client-in-client – Daniele Vrut Mar 03 '14 at 13:39
  • after hours of smashing head, I just made it with this trick **var sesid = client.handshake.headers.cookie.replace('PHPSESSID=','');**. as page is rendered with PHP, so get's PHP's sessionID. – Airy Mar 03 '14 at 14:37
  • And Socket.io creates a new ID everytime the page is called as we make another total new request to node. So, in our case, it is almost useless for me to use Node's SessionID. While the PHP's Session ID is same through out the browser. – Airy Mar 03 '14 at 14:40
0

You can use Long Polling technique. Long polling is the name used to describe a technique which:

  1. An AJAX request is made (utilizing a javascript framework such as jQuery)
  2. The server waits for the data requested to be available, loops, and sleeps (your server-side PHP script)
  3. This loop repeats after data is returned to the client and processed (usually in your AJAX request's onComplete callback function)

or

Try a server side pushing tech like Commet.

Konsole
  • 3,447
  • 3
  • 29
  • 39