PHP application + WebSocket based notifications over Socket.IO

22,880

Solution 1

This tutorial on using PHP and Socket.IO answers the main part of your question.

socket.io isn't your only options. Have a look at the realtime web tech guide. By the looks of it the best PHP-based solution right now is Ratchet. This needs to run as a separate process so you're still going to have the cross-process communication problem.

The common cross process communication problem is to use messages queues. The problem then comes in when you want your PHP app running on Apache to be informed of events from the realtime web technology. You're probably going to have to do that via a web call, even if it's a call from the realtime web tech process to the apache server.

Another option is to offload the realtime communication to a hosted service like Pusher who I work for.

This recent question is also relevant and has some useful links: Is native PHP support for Web Sockets available?

Solution 2

Elephant.IO supports PHP with Socket.IO so take a look at it!

Solution 3

There isn't an easy way to communicate between node and apache/php. So you're either going to have to write all of your functionality again in node (connecting to mysql databases and such) or create an API to call from Node and pass to sockets.

However you could use a jquery plugin like Jquery graceful websockets and this php class to get your own websockets functionality with jquery and php, rather than using sockets.io.

Share:
22,880
tomsseisums
Author by

tomsseisums

Updated on February 04, 2021

Comments

  • tomsseisums
    tomsseisums over 3 years

    I have a web application written in PHP using laravel.

    Now, I have to add realtime notification system that pushes messages from server -> client, and can retrieve messages pushed from client -> server.

    Since Socket.IO is backwards compatible and degrades gracefully, I want to use it for the WebSocket / AJAX polling part.

    The problem is that Socket.IO by default works with Node.JS backend.

    I have root access to the server, there is no problem running both (Apache and Node) in parallel or adding anything extra.

    The problem is, how do I send a message from PHP to Node + Sockets.IO?

    And then, how do I transfer a message from Sockets.IO + Node to PHP?

    I have left the Node + Sockets to Browser part out, because that's pretty straight forward.

    I have seen examples utilising Express.js + CURL and/or POST requests, but, isn't there a lower level way of doing it?

    I am aware of DNode + PHP, but I cannot imagine how to assemble everything - Apache, PHP, Node, Socket.IO, DNode so it would work together seamlessly.

    Also, I have transformed into a laravel library this PHP WebSockets server implementation. The problem with this library, though, is that it utilizes clean WebSockets for it's client side. And, I couldn't find a way to hack it together with Socket.IO client.

  • tomsseisums
    tomsseisums over 11 years
    Damn, that Ratchet! It owns! socketo.me/docs/push has the step-by-step tutorial on how to push from your non-ratchet app. I implemented the functionality in minutes. +10 for Ratchet.
  • Primoz Rome
    Primoz Rome about 11 years
    @leggetter Wow, Pusher looks like a real no-brainer here to me. Thanks for sharing this. I think I am gonna give it a try. Better this then wasting my time implementing my own system.
  • Ivo Renkema
    Ivo Renkema almost 11 years
    Yeah, Pusher.com seems to be the sane route to go!
  • Placeholder
    Placeholder over 9 years
    This doesn't solve the main request of the question: USING Socket.IO
  • leggetter
    leggetter over 9 years
    @Hellen I've added a link to an article that does directly answer how to integrate Socket.IO and PHP.