Using WebSocket on Apache server

69,596

Solution 1

One path is to use an independent installed web sockets server.

For PHP you can try: http://code.google.com/p/phpwebsocket/ or http://github.com/Devristo/phpws/

There are some other projects which you can try as well.

Basically, you need to upload, unpack and start running the process.

On the frontend, you'll have javascript connecting to the server on the specific port.

Most websocket servers have a demo which echoes back whatever it hears, so this is a good place to write some test code. You may even find a rudimentary chat implementation.

The tricky part is to monitor the web socket server and to make sure it runs smoothly and continuously.

Try to test on as many browsers/devices as possible as this will decide on which websocket server implementation you choose. There are old and new protocols you have to watch out for.

Solution 2

As @zaf states you are more likely to find a standalone PHP solution - not something that runs within Apache. That said there is a apache WebSocket module.

However, the fundamental problem is that Apache wasn't built with maintaining many persistent connections in mind. It, along with PHP, is built on the idea that requests are made and responses are quickly sent back. This means that resources can very quickly be used up if you are holding requests open and you're going to need to look into horizontal scaling pretty quickly.

Personally I think you have two options:

  1. Use an alternative realtime web technology solution and communicate between your web application and realtime web infrastructure using queues or short-lived requests (web services).
  2. Off load the handling of persistent connections and scaling of the realtime web infrastructure to a realtime web hosted service. I work for Pusher and we fall into this category.

For both self-hosted and hosted options you can check out my realtime web tech guide.

Solution 3

I introduced another websocket server: PHP Ratchet (Github).

This is better and complete list of client & server side codes and browser support.

Please check this link.

Share:
69,596
Edouard Reinach
Author by

Edouard Reinach

Creative Technologist & innovation lead Long-time Front-end developer enthusiast

Updated on June 21, 2020

Comments

  • Edouard Reinach
    Edouard Reinach almost 4 years

    With all the buzz around WebSockets, it's pretty hard to find a good walkthrough on how to use them with an Apache server on Google.

    We're developing a plugin, in PHP (symfony2), which will run from time to time kind of a chat instance. And we find WebSockets more interesting, standard and quick than AJAX for this matter. The thing is, we don't have much sysadmin ressources in our group and we find hard to gather good informations on the following matters:

    • Can we run a WebSocket instance on a traditional Apache, dedicated server, and if yes, do you have useful links for us?
    • If we need to mod the server, what kind of tools would you recommend knowing that we are not too skilled in sysadmin so we can't afford to have a high maintenance b*** on this.

    Thank you very much,

    ps: we'll link back to your blog/site as we'll make a technical/informational post on our devblog about this part of our app.

    Thank you again!

  • Edouard Reinach
    Edouard Reinach almost 12 years
    Thank you very much. Do you have any monitoring tool in mind by chance? Have a great time!
  • Edouard Reinach
    Edouard Reinach almost 12 years
    Very interesting stuff :) thank you very much! I love your guide, though it's not helping much to differenciate between solutions. We'll check documentation ;) thanks again!
  • zaf
    zaf almost 12 years
    We have our own light weight monitoring tools. For starters a simple cron script that checks when the process is not running and restarts it. You can build up from that or search for ready made process monitoring apps.
  • leggetter
    leggetter almost 12 years
    I do need to find the time to try out all the technologies and provide some guidance on the pros/cons of the different options. For now it's just a list to click-through - a starting point for your own investigations.
  • Edouard Reinach
    Edouard Reinach almost 12 years
    Ok, I'm sure our dev team is gonna be able to do something! Thanks again!
  • Ben
    Ben almost 10 years
    Thanks, but <i>why</i> is it better?
  • ShahRokh
    ShahRokh almost 10 years
    Becuase it's FAST & EASY & React subset(reactphp.org). It has good documents & support.
  • Tebe
    Tebe over 8 years
    can we have a look at any tests?