Is native PHP support for Web Sockets available?

67,116

Solution 1

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:

Note: IE10 is now released in Windows 8

Also see: Ajax push system

Solution 2

Yes, it's possible to do PHP + Websocket very simply, without any third party library (like Ratchet, often mentioned).

This article is a great lightweight example. (I lost hours with complex solutions, all of them including a few libraries, until I found this useful, simple, article)

You can find more detailed instructions about this here: How to create websockets server in PHP.

It uses a constantly-running PHP server, that you start from command-line with php websockets.php, with an event-loop (similar to the Node.JS way). It's 100% possible to use native PHP functions like socket_create, etc.

Share:
67,116

Related videos on Youtube

Kev
Author by

Kev

###Actively looking for freelance work ###About Me: I'm a professional software developer and have spent my time building provisioning and web based self-service systems for IIS, Apache and Citrix XenServer, amongst other things. My Curriculum Vitae can be viewed on Stack Overflow Careers (might be a bit out of date). Stuff I like to listen to at last.fm You can get in touch here: kevin.e.kenny #@# gmail.com (you know what to do with the # and spaces). No Survey Emails Please. Also not ashamed to admit I like trains, mostly diesels, late Era 8 (BR Sectorisation) and Era 9 onwards :) I'm also interested in signalling if anyone from Network Rail is looking this far down ;)

Updated on July 05, 2022

Comments

  • Kev
    Kev almost 2 years

    Looking for Hello World Type Example of Web Sockets Implementation:

    Here is Socket Create reference from php.net but this looks more low level than Web Sockets.

    I want to use this Web Sockets as shown here on caniuse.com which is now implemented in all new major browsers.

    A Google search turned up this Nets.TutsPlus site in which I can use the JavaScript example code...but I need to know how to implement the server-side in PHP not Java, Ruby, or Node.js as in the example.

    Is PHP Socket Create relevant? Does PHP natively support Web Sockets? I guess just a point in the right direction for PHP implementation would help.

    Actually the tutorial has a broken link to phpwebsockets...is this the library one should use?

    Websockets.org has a test application, but no mention of PHP.

  • Admin
    Admin over 11 years
    Do host providers support this? small ones...like godaddy.com, wiredtree.com, do I essentially just pick a library and then drop it into my web folder...thing is I'm on a LAMP stack...
  • leggetter
    leggetter over 11 years
    The general answer to this is that hosting providers don't like you creating persistent connections - and definitely not potentially 100s or 1000s of persistent connections. Personally I don't think self-hosted WebSocket usage on shared hosting is going to be an option for quite a while. It certainly doesn't integrate particularly nicely with the standard LAMP stack IMHO. For now, I think a hosted service such as Pusher (who I work for) is the best solution for developers on a shared hosting LAMP stack.
  • Admin
    Admin over 11 years
    I checked with Technical Support at Wired Tree and they said it was fine.
  • Admin
    Admin over 10 years
    @leggetter can you provide a link on persistent connection hate? my bandwidth usage plummeted when I switched to 100% ajax. It fell at about the same rate after going 100% websocket. So I'm very curious. Thanks in advance!
  • leggetter
    leggetter over 10 years
    @Gracchus "persistent connection hate"? If you can please clarify I'll do what I can to help.
  • Admin
    Admin over 10 years
    @leggetter was just trying to conserve space. re: "The general answer to this is that hosting providers don't like you creating persistent connections - and definitely not potentially 100s or 1000s of persistent connections." the only thing that occurred to me was that it's assumed websockets would use more bandwidth, but i use much less bandwidth with websockets vs ajax vs postback. was wondering if you had details on that comment. thanks again in advance!
  • leggetter
    leggetter over 10 years
    @Gracchus the problem isn't the bandwidth, it's the number of persistent connections. In general, it's the cost of maintaining the connections that really eats up resource usage. If you take this into consideration when you're using shared hosting then clearly having one application with lots of persistent connections can impact other applications on the shared host. PaaS providers such as Heroku and OpenShift have only relatively recently adding WebSocket support - Heroku are still in beta. OpenShift wrote about some of the challenges they faced openshift.com/blogs/paas-websockets
  • Admin
    Admin over 10 years
    @leggetter can you give a link about the resource usage of persistent connections? i didn't see it in the link you provided, but thank you for showing me the pain languages like php, ruby, etc have to go through to get up and running with apache! thank you very much again in advance!
  • leggetter
    leggetter over 10 years
    @Gracchus No link to a write-up that answers your question directly I'm afraid. You can piece together the bits of the puzzle by reading up on Apache MPM modules this is a good start (see Prefork in particular), Apache MPM event module, older resources and newer ones. Sorry, they don't directly answer the question.
  • JSON
    JSON over 10 years
    @Gracchus - What leggetter is saying is pretty much common knowledge. Opening sockets to the outside world comes with many problems, especially when the service is often engineered by novice coders. You have process managing issues, connection management issues, security issues (each open port is basically a door into the system), etc. In addition, if the service isn't broken, isn't riddled with security issues, and runs efficiently, it basically becomes a competing service for the provider. Not many business models are open to such a thing.
  • YakovL
    YakovL over 7 years
    For which versions of PHP this is true? Is this still correct for the latest ones?
  • Rusty
    Rusty over 6 years
    A really good article/tutorial which demonstrates on how to use PHP websockets with 'ghedipunk/PHP-WebSockets', Elephant.io, Rachet and Wrench
  • Alcalyn
    Alcalyn over 6 years
    I also made Sandstone: github.com/eole-io/sandstone and I use it when I need a websocket server with a rest api and push events
  • aariow
    aariow about 3 years
    This is exactly what I want !. THANK YOU DEAR. I'm really interested in how things work in low-level programming.
  • Adriano
    Adriano almost 3 years
    It is a nice project. You can't start it or call the socket if you are using an unsecure http page. So this example just works on unsecure connections (not on https)