Websockets & PHP

I'm starting to see websockets as a solution to replace the long poll in a new PHP build application. I'm getting to work.

I have a few questions that I wonder if people can help me.

  • Can a Nodejs server call PHP, and if that happened, would it have the same flaws as just going through Apache in terms of connections? We all know that nodejs is not blocking, but Apache, etc. - this is not so, but if Nodejs just makes a call to the PHP server in its own procedure, is it not like a bottle of neck in a similar way?
  • Are PHP and websockets a good match?
  • Are there any good js libraries besides socketio that apparently only work with Nodejs?
  • Has anyone found a good tutorial that uses websockets and a PHP backend, perhaps using something like this PHP Ratchet library that can help me get in my way?

Thoughts would be greatly appreciated.

+4
source share
2 answers

Please excuse my retelling of your questions.

1: Can Node.js call PHP and won't have the same flaws as Apache?

Invoking a one-time PHP script will have the same common drawbacks as invoking a web page, except that you remove the extra layer of processing. Apache or any web server itself is such a thin layer that although you save some time, the savings will be negligible.

If PHP is more efficient at collecting data for your clients than Node.js, for some reason it might be wise to include PHP in your application.

2: Is there a good match between PHP and WebSockets?

Traditional PHP scripts usually run once per request. The vast majority of PHP developers are not familiar with the development of events, and PHP itself does not yet support asynchronous processing.

PHP is a fast, mature scripting language that is only accelerating, even with all its many warts and flaws. (Some say that its weak typing is a flaw. Others say that it is a flaw, that its typing is not weak enough.)

However, the minimum requirement of any language for implementing WebSockets is the ability to open a basic TCP port and listen for requests. For PHP, it is implemented as a thin shell of the C socket library, and additional extensions and frameworks are also available that can also change the way TCP sockets work using PHP.

The PHP garbage collector has also matured. Memory leaks occur either from gross neglect of memory (I look at you, on the Zend Framework), or to the deliberate sabotage of the garbage collection system by developers who think they are smart or want to prove how easy it is to defeat the GC. (Spoiler: easy in every language if you know the details!)

In PHP, it is quite possible and very easy to configure a daemon (a lengthy background process). It even allows you to make it good enough to gracefully restart and transfer your connections to a new version of the same script or even the same script on the same server that runs different versions of PHP, although this only recedes a tiny bit.

As for good compliance, it is entirely up to the developer. Are you ready, able and happy to work with PHP to write a WebSockets server or to use one of the existing servers? Yes? Then you fit PHP and WebSockets well.

3: JS Libraries for WebSockets

I honestly did not study them.

4: Tutorials on Using PHP and Websites

I personally love this tutorial: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html

Although I am sure that the specification of this tutorial will soon be obsolete for this particular WebSockets server. (However, there will still be an active branch for this server.)

In the case of a rot connection:

Using the PHP-Websockets server (available on Github, will be enabled soon), extend the WebSocketServer base class and implement the abstract methods process() , connected() and closed() .

There is much better information on the link above, so stay tuned for as long as the link exists.

+1
source
  • If you go through apache, this will be the same bottleneck. This can be fixed using another web server, such as lighthttpd or nginx . You don’t even need node.
  • PHP does not have decent shared memory , which makes the biggest benefits of WebSockets irrelevant. It should be decent enough if you do not want to interact between users, but even then I would have to frown on using PHP. PHP is great for many things, but real-time communication is not one of them.
  • You might want to check out https://github.com/einaros/ws .
  • PHP is not a good background. Everything that has a runtime model that does not start and is not forgotten in its own sandbox, such as Node , .NET , C / C ++ and Java - are good matches. PHP is suitable for short starts, such as actual websites and even web services, but not real-time connections.
0
source

All Articles