83

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.

7

2 Answers 2

53

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

13
  • 6
    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...
    – user656925
    Aug 31, 2012 at 14:00
  • 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.
    – leggetter
    Aug 31, 2012 at 14:48
  • 2
    I checked with Technical Support at Wired Tree and they said it was fine.
    – user656925
    Sep 1, 2012 at 21:33
  • 1
    @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!
    – user1382306
    Nov 2, 2013 at 16:36
  • 5
    @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.
    – JSON
    Jan 17, 2014 at 7:57
12

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.

2
  • 1
    This is exactly what I want !. THANK YOU DEAR. I'm really interested in how things work in low-level programming.
    – aariow
    Apr 18, 2021 at 18:46
  • 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)
    – Adriano
    Aug 6, 2021 at 10:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.