PHP WebSockets

(This feature is currently disabled on my website sorry)

I’ve added a PHP WebSockets solution that runs across every page on this site. If server resources are free and you have port 8080 open on your firewall, you will see a bubble in the lower left hand corner of this web page (or any webpage on this website). If you click the bubble you can type a emoticon smiley face with your keyboard and launch your emoticon bubble across the webpage. The neat thing is, every person who is currently visiting the website will see the same bubble flying. You can test this out but opening several browser windows at once and typing into one of the browser windows. All windows will show the activity. This is a very interactive way of engaging visitors where the website is given more of a presence in “physical” space because it has this live quality that’s universal. WebSockets could be used for gaming, and with html5 the possibilities are endless.

How it works

Two files and a process make this happen.

  1. /js/bubble.js – This is the client-side javascript file that is included with every webpage. Bubble attempts a socket handshake, on success it reveals itself to the browser and starts working. If the server stops the socket, bubble hides itself and sleeps before retrying the connection again.
  2. server.php – A server.php file lives on the server. This doesn’t have to be in PHP but I was curious. It’s job is simply to collect and relay all incoming keyboard data to all available browser windows. This is the server-side of websockets.
  3. Cron job – Although not a file, this is a very important part of websockets. Since PHP eats so much memory I have this cron job run every hour. The server.php file kills all previous server.php processes and takes over as the new socket server. Each bubble.js client is notified and the bubble.js reestablishes a connection. This could be managed through a Daemon as well. For a commercial application of this I would choose a different language for the suexec protection and memory. Adding a dedicated IP would give it the SSL port for the firewall limitations too.

Lives on Board

The server.php file keeps each client notified of the total browser windows open or “Lives on Board”. When one browser closes, each client is notified and the total is updated on each screen. The server stores this in a static file but it could by in a database. You can see it here: lob.nv.


«     »
Categorized in: ... June 6, 2015 3:42 am

Leave a Reply

Your email address will not be published. Required fields are marked *