Is Native PHP Support For Web Sockets Available

Is native PHP support for Web Sockets available?

There isn't native support in terms of there being a standard PHP WebSocket object natively available.

You'll need to use a library.

The next thing to consider is how the WebSocket server runs. Normally PHP runs in Apache, Nginx (via FastCGI) or on Microsoft IIS (via Fast CGI). With Apache and IIS this may be a problem as it's not really built with persistent connections such as WebSockets in mind. I'm not sure about Nginx. This is why most PHP WebSocket libraries will be built as standalone libraries to be run as their own processes.

See:

  • Apache Module: https://github.com/disconnect/apache-websocket
  • Ratchet: https://github.com/cboden/Ratchet
  • Wrench: https://github.com/varspool/Wrench
  • PHP WebSocket: http://code.google.com/p/phpwebsocket/

Note: IE10 is now released in Windows 8

Also see: Ajax push system

How to create websockets server in PHP

I was in the same boat as you recently, and here is what I did:

  1. I used the phpwebsockets code as a reference for how to structure the server-side code. (You seem to already be doing this, and as you noted, the code doesn't actually work for a variety of reasons.)

  2. I used PHP.net to read the details about every socket function used in the phpwebsockets code. By doing this, I was finally able to understand how the whole system works conceptually. This was a pretty big hurdle.

  3. I read the actual WebSocket draft. I had to read this thing a bunch of times before it finally started to sink in. You will likely have to go back to this document again and again throughout the process, as it is the one definitive resource with correct, up-to-date information about the WebSocket API.

  4. I coded the proper handshake procedure based on the instructions in the draft in #3. This wasn't too bad.

  5. I kept getting a bunch of garbled text sent from the clients to the server after the handshake and I couldn't figure out why until I realized that the data is encoded and must be unmasked. The following link helped me a lot here: (original link broken) Archived copy.

    Please note that the code available at this link has a number of problems and won't work properly without further modification.

  6. I then came across the following SO thread, which clearly explains how to properly encode and decode messages being sent back and forth: How can I send and receive WebSocket messages on the server side?

    This link was really helpful. I recommend consulting it while looking at the WebSocket draft. It'll help make more sense out of what the draft is saying.

  7. I was almost done at this point, but had some issues with a WebRTC app I was making using WebSocket, so I ended up asking my own question on SO, which I eventually solved: What is this data at the end of WebRTC candidate info?

  8. At this point, I pretty much had it all working. I just had to add some additional logic for handling the closing of connections, and I was done.

That process took me about two weeks total. The good news is that I understand WebSocket really well now and I was able to make my own client and server scripts from scratch that work great.
Hopefully the culmination of all that information will give you enough guidance and information to code your own WebSocket PHP script.

Good luck!


Edit: This edit is a couple of years after my original answer, and while I do still have a working solution, it's not really ready for sharing. Luckily, someone else on GitHub has almost identical code to mine (but much cleaner), so I recommend using the following code for a working PHP WebSocket solution:

https://github.com/ghedipunk/PHP-Websockets/blob/master/websockets.php


Edit #2: While I still enjoy using PHP for a lot of server-side related things, I have to admit that I've really warmed up to Node.js a lot recently, and the main reason is because it's better designed from the ground up to handle WebSocket than PHP (or any other server-side language). As such, I've found recently that it's a lot easier to set up both Apache/PHP and Node.js on your server and use Node.js for running the WebSocket server and Apache/PHP for everything else. And in the case where you're on a shared hosting environment in which you can't install/use Node.js for WebSocket, you can use a free service like Heroku to set up a Node.js WebSocket server and make cross-domain requests to it from your server. Just make sure if you do that to set your WebSocket server up to be able to handle cross-origin requests.

How to bind to web sockets server from old browser that does not support HTML5

Socket.io suports the most browsers, but unfortunately not all.

websocket in php wont connect to server.php

The code you've written here is not correctly negotiating a WebSocket connection. You will need* to use a WebSocket library to do that; see "Is native PHP support for Web Sockets available?" for a list of PHP WebSocket libraries.

*: While it's technically possible to write a client yourself based on the specification, it's rather complex, making it inadvisable to do so yourself unless you have some very unusual requirements.

Do we need a static IP to use HTML5 Websockets?

You must be able to connect to the port on the server where the WebSocket server is running. If you are using a port other than 80 or 443 on your site for the WebSocket server, then you probably need to configure (or ask) the site to accept incoming connections on that port (because they may deny it by default for security reasons). It you are unable to telnet to the WebSocket port on the server, then this is likely the case (or phpwebsocket is not in fact correctly configured to list on that port).

Using / Implementation of Web Sockets Server side

Most of the server-side options you mention in your question are related to the same technology, namely node.js.

  1. node.js is a server-side JavaScript solution based on Google's v8 engine designed for building scalable web-applications
  2. npm is a package manager for node which allows you to install and publish node programs and manage their dependencies. It's similar in some ways to the package managers available on some linux distributions (rpm and apt for example).
  3. socket.io is a plug-in for node.js for developing real time applications. It selects the most suitable transport mechanism for the client but hides the details behind a single API.

If you're building a real-time application I can definitely recommend looking in more detail at node and socket.io.

The node download page has installers for Windows and Mac, and there are various guides to installing node on Ubuntu.



Related Topics



Leave a reply



Submit