86

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
    This can probably help: PHP Socket Programming, done the Right Way Commented Aug 30, 2012 at 18:46
  • 2
    Googling "PHP Websockets" gives you many results such as: socketo.me, flynsarmy.com/2012/02/php-websocket-chat-application-2-0, code.google.com/p/phpwebsocket Commented Aug 30, 2012 at 18:52
  • 1
    I don't see a clear direction on how to approach Web Sockets in PHP....is it natively supported or do I need another library...would be my first question on how to implement?.... Commented Aug 30, 2012 at 18:55
  • 1
    In your searching, don't confuse sockets with web sockets. Commented Aug 30, 2012 at 18:59
  • 2
    Excellent question. This is what I want to ask. Commented Apr 8, 2013 at 6:35

2 Answers 2

54

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

Sign up to request clarification or add additional context in comments.

13 Comments

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...
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.
I checked with Technical Support at Wired Tree and they said it was fine.
@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!
@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.
|
13

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.

4 Comments

This is exactly what I want !. THANK YOU DEAR. I'm really interested in how things work in low-level programming.
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)
Is this still available? Because I can't see any codes in that article currently. Do you have those files with you?
I would also like the source from that page, as it's empty.

Your Answer

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