I am trying to implement my sites little chat function like facebook uses. I used Html, PHP, JQuery and Apache. I made before some exaples with JMS but I couldn't figure out how to send message to a JMS server with PHP?
For JMS I used Weblogic.
You have a few options. Let's go through them in order of worst to not-worst.
First is Quercus, a PHP implementation in Java. PHP code running in Quercus can instantiate Java classes and call methods on them, just like they were PHP objects. You'd be able to work with JMS in the same way you'd work with it in Java. I have no experience with this one.
Second is the PHP/Java Bridge. It's a bit more complex to get working, but it effectively works the same way as Quercus, code-wise. You'd be able to work directly with any old Java API. I have experience with this one, and it wasn't pleasant. Java processes ended up hanging around in a zombie state after the corresponding Apache+PHP child exited. We eventually had to write a cron script to clean up all working Java processes nightly, otherwise the server would eventually run out of available memory.
Third would be looking at different JMS providers, like ActiveMQ. Why? Because ActiveMQ supports many messaging APIs, including Stomp, a language-agnostic messaging API that happens to have a PHP extension. It's possible that Weblogic can speak Stomp, but I was unable to determine if this is the case. The only Google results were for Weblogic + ActiveMQ integration.
Fourth would be not thinking about JMS. JMS is a Java solution. PHP ain't Java. There are plenty of message queue systems out there that work well with PHP. ActiveMQ is only one example. I personally prefer Gearman. Gearman was originally designed to perform sharded remote procedure calls, but it works equally as well for simple message passing, and has a PHP extension as well as bindings for most other languages under the sun.
Just to give two other options: OpenMQ (using HTTP) and PHPMQ
(My two cents about the accepted answer)
Great answer, one quibble: Gearman is great for Remote Procedure Calls, but as to message passing, JMS is much more robust (persistence, etc.). Aim for JMS in some way would be my suggestion.
Related
In the application I've written there is a whole class that only takes in some parameters, eventually fetches the db for other parameters, and then returns the results to the asking class. It only handles data, so it would be perfect to "outsurce" those functions to something more efficient than PHP.
I've read the official php faq page on "PHP and COM", and seen that it is not possible to run a precompiled dll from php.
So, do you think it is possible and worth to do it with some other methods? And how would you do it?
Maybe PHP/Java Bridge / RESTful WS could do, but still java does not sound like the best option (according to this post it's "clunky")
HipHop would be a great solution, but as of now it's way too early to use it in production for my needs.
All the solutions listed here seem to be aimed at creating an .exe file for making a standalone win app, whereas my app is hosted on a centOS server and I haven o need to port it to windows (or at least that's not my intention right now).
I use C++ and RabbitMQ to tie it together. I've also had success, when there wasn't much of a scope for the project, at using Redis as a tie in.
Can't say enough good things about using a message bus to separate processes (like RabbitMQ).
If you're just getting started with RabbitMQ, I'd highly recommend RabbitMQ in Action.
You don't have to use C++ though. That's one of the major benefits of using message buses; you can substitute later, if need be (and the API stays the same). Here are a list of clients that can receive and post messages to RabbitMQ: http://www.rabbitmq.com/devtools.html
Insert favorite, high-performance language here.
I'm starting to consider websockets as a solution to replace long polling in a new build PHP app I am commissioning.
I have a few questions which I wonder if people could help me out with.
Can a Nodejs server call PHP and if it did wouldn't it suffer the same shortcomings as just going through Apache in terms of the connections? We all know nodejs is non blocking and Apache etc isn't but if Nodejs is just making a call to a PHP server in it's own procedure would that not bottle neck in a similar way?
Are PHP and websockets a good match?
Are there any good js libraries besides socketio which apparently only works with Nodejs?
Has anyone found a good tutorial which uses websockets and a PHP backend maybe using something like that Ratchet PHP library which might help me get on my way?
Thoughts would be muchly appreciated.
Please excuse my paraphrasing of your questions.
1: Can Node.js call PHP, and wouldn't that have the same shortcomings as Apache?
Calling a run-once PHP script will have the same general shortcomings as calling a web page, except that you are removing an extra layer of processing. Apache or any web server itself is such a thin layer that, while you'll save some time, the savings will be insignificant.
If PHP is more effective at gathering data for your clients than Node.js, for whatever reason, then it might be wise to include PHP in your application.
2: Are PHP and WebSockets a good match?
Traditional PHP scripts are normally intended to be run once per request. The vast majority of PHP developers are unfamiliar with event driven development, and PHP itself does not (yet) have support for asynchronous processing.
PHP is a fast, mature scripting language that is only getting faster, even with all of its many warts and shortcomings. (Some say that its weak typing is a shortcoming. Others say that it's a shortcoming that its typing isn't weak enough.)
That said, the minimum that any language needs in order to implement WebSockets is the ability to open up a basic TCP port and listen for requests. For PHP, it is implemented as a thin wrapper around the C sockets library, and there are additional extensions and frameworks available that can also change the feel of working in TCP sockets with PHP.
PHP's garbage collector has also matured. Memory leaks come either from gross disregard for the memory space (I'm looking at you, Zend Framework) or from intentional sabotage of the garbage collection system by developers who think they're clever or want to prove how easy it is to defeat the GC. (Spoiler: It's easy in every language, if you know the details!)
It is quite possible and very easy to set up a daemon (long running background process) in PHP. It's even possible to make it well behaved enough to gracefully restart and hand its connections off to a new version of the same script, or even the same script on the same server running different versions of PHP, though this is treading out of scope just a tiny little bit.
As for whether it's a good match, that is completely up to the developer. Are you willing, able, and happy to work with PHP to write a WebSockets server, or to use one of the existing servers? Yes? Then you're a good match for PHP and WebSockets.
3: JS Libraries for WebSockets
I honestly haven't researched them.
4: Tutorials for using PHP and Websockets
I'm personally fond of this tutorial: http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html
Although I have it on good authority that the specifics of that tutorial will soon be obsolete for that specific WebSockets server. (There will still be an actively maintained legacy branch for that server, though.)
In case of link rot:
Using the PHP-Websockets server (available on Github, will be homed soon), extend the base WebSocketServer abstract class and implement the abstract methods process(), connected(), and closed().
There's much better information at the link above, though, so follow it as long as the link exists.
It would hit the same bottleneck if you go through apache. This can be remedied by using a different web server, such as lighthttpd or nginx. You won't even need node at all.
PHP does not have decent shared memory making the biggest advantages of a WebSockets irrelevent. It should be decent enough if you don't want interaction between users, but even then I would have to frown upon the usage of PHP. PHP is great for a lot of things, but real-time communication is not one of them.
You might want to look at https://github.com/einaros/ws.
PHP is not a good back-end. Anything with an execution model that isn't run-and-forget in its own sandbox, such as Node, .NET, C/C++ and Java are good matches. PHP is suited for short running executions, such as actual web sites and even web services -- but not real time connections.
I'm working on a web interface to pull information from storage arrays. I'd like to use SMI-S There are Java CIM clients I can use (SBLIM CIM Client), but I'd really like there to be a way for PHP to pull information via CIM. Can anybody point me in the right direction? Is there a way I can, say, send a request over http (port 5988) and just parse out an xml response?
If anybody can suggest a better way that'd be great as well. Thank you.
I don't know of a CIM client written directly in PHP, but normally you can integrate packages written in Perl or Python into your server side PHP.
You could also integrate a command line CIM client into PHP, e.g. cimcli from OpenPegasus, but parsing the output of a command line client may not really be convenient.
I would recommend against writing your own CIM-XML parser - there are many details to observe in the specs (DSP0200, DSP0201), this would definitely be a major undertaking.
For Python, there is the PyWBEM CIM client which works very well, even though there is not much activity on the project. Also, there is the PowerCIM CIM client for Python, which is newer but I did not use it yet. PowerCIM provides the CIM classes as Python classes, while PyWBEM provides generic Python classes such as CIMInstance or CIMClass. Just two different styles to deal with, for integration into a next higher layer such as PHP it seems to me you'd be better off with the generic style provided by PyWBEM.
There is a Perl interface on top of the OpenWEM client, but I don't have experience with that.
Andy
SMI-S calls to get array details may use GetInstances() or EnumerateInstances(), which are basically predefined methods. These methods come with library such as OpenPegasus. If you are successful in including the required libraries, then you can create the client variable, connect to the CIM server and invoke the required method.
If you are specifically using OpenPegasus, you may need to know how to use C++ in PHP as the former is implemented in C++. Refer http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/
I am currently trying to implement a job queue in php. The queue will then be processed as a batch job and should be able to process some jobs in parallel.
I already did some research and found several ways to implement it, but I am not really aware of their advantages and disadvantages.
E.g. doing the parallel processing by calling a script several times through fsockopen like explained here:
Easy parallel processing in PHP
Another way I found was using the curl_multi functions.
curl_multi_exec PHP docs
But I think those 2 ways will add pretty much overhead for creating batch processing on a queue that should mainly run on the background?
I also read about pcntl_fork which also seems to be a way to handle the problem. But that looks like it can get really messy if you don't really know what you are doing (like me at the moment).
I also had a look at Gearman, but there I would also need to spawn the worker threads dynamically as needed and not just run a few and let the gearman job server then sent it to the free workers. Especially because the threads should be exit cleanly after one job has been executed, to not run into eventual memory leaks (code may not be perfect in that issue).
Gearman Getting Started
So my question is, how do you handle parallel processing in PHP? And why do you choose your method, which advantages/disadvantages may the different methods have?
i use exec(). Its easy and clean. You basically need to build a thread manager, and thread scripts, that will do what you need.
I dont like fsockopen() because it will open a server connection, that will build up and may hit the apache's connection limit
I dont like curl functions for the same reason
I dont like pnctl because it needs the pnctl extension available, and you have to keep track of parent/child relations.
never played with gearman...
Well I guess we have 3 options there:
A. Multi-Thread:
PHP does not support multithread natively.
But there is one PHP extension (experimental) called pthreads (https://github.com/krakjoe/pthreads) that allows you to do just that.
B. Multi-Process:
This can be done in 3 ways:
Forking
Executing Commands
Piping
C. Distributed Parallel Processing:
How it works:
The Client App sends data (AKA message) “can be JSON formatted” to the Engine (MQ Engine) “can be local or external a web service”
The MQ Engine stores the data “mostly in Memory and optionally in Database” inside a queues (you can define the queue name)
The Client App asks the MQ Engine for a data (message) to be processed them in order (FIFO or based on priority) “you can also request data from specific queue".
Some MQ Engines:
ZeroMQ (good option, hard to use)
a message orientated IPC Library, is a Message Queue Server in Erlang, stores jobs in memory. It is a socket library that acts as a concurrency framework. Faster than TCP for clustered products and supercomputing.
RabbitMQ (good option, easy to use)
self hosted, Enterprise Message Queues, Not really a work queue - but rather a message queue that can be used as a work queue but requires additional semantics.
Beanstalkd (best option, easy to use)
(Laravel built in support, built by facebook, for work queue) - has a "Beanstalkd console" tool which is very nice
Gearman
(problem: centralized broker system for distributed processing)
Apache ActiveMQ
the most popular open source message broker in Java, (problem: lot of bugs and problems)
Amazon SQS
(Laravel built in support, Hosted - so no administration is required. Not really a work queue thus will require extra work to handle semantics such as burying a job)
IronMQ
(Laravel built in support, Written in Go, Available both as cloud version and on-premise)
Redis
(Laravel built in support, not that fast as its not designed for that)
Sparrow
(written in Ruby that based on memcache)
Starling
(written in Ruby that based on memcache, built in twitter)
Kestrel
(just another QM)
Kafka
(Written at LinkedIn in Scala)
EagleMQ
open source, high-performance and lightweight queue manager (Written in C)
More of them can be foun here: http://queues.io
If your application is going to run under a unix/linux enviroment I would suggest you go with the forking option. It's basically childs play to get it working. I have used it for a Cron manager and had code for it to revert to a Windows friendly codepath if forking was not an option.
The options of running the entire script several times do, as you state, add quite a bit of overhead. If your script is small it might not be a problem. But you will probably get used to doing parallel processing in PHP by the way you choose to go. And next time when you have a job that uses 200mb of data it might very well be a problem. So you'd be better of learning a way that you can stick with.
I have also tested Gearman and I like it a lot. There are a few thing to think about but as a whole it offers a very good way to distribute works to different servers running different applications written in different languages. Besides setting it up, actually using it from within PHP, or any other language for that matter, is... once again... childs play.
It could very well be overkill for what you need to do. But it will open your eyes to new possibilities when it comes to handling data and jobs, so I would recommend you to try Gearman for that fact alone.
Here's a summary of a few options for parallel processing in PHP.
AMP
Checkout Amp - Asynchronous concurrency made simple - this looks to be the most mature PHP library I've seen for parallel processing.
Peec's Process Class
This class was posted in the comments of PHP's exec() function and provides a real simple starting point for forking new processes and keeping track of them.
Example:
// You may use status(), start(), and stop(). notice that start() method gets called automatically one time.
$process = new Process('ls -al');
// or if you got the pid, however here only the status() metod will work.
$process = new Process();
$process.setPid(my_pid);
// Then you can start/stop/check status of the job.
$process.stop();
$process.start();
if ($process.status()) {
echo "The process is currently running";
} else {
echo "The process is not running.";
}
Other Options Compared
There's also a great article Async processing or multitasking in PHP that explains the pros and cons of various approaches:
pthreads extension (see also this SitePoint article)
Amp\Thread Library
hack's async (requires running Facebook's HHVM)
pcntl_fork
popen
fopen/curl/fsockopen
Doorman
Then, there's also this simple tutorial which was wrapped up into a little library called Doorman.
Hope these links provide a useful starting point for more research.
First of all, this answer is based on the linux OS env.
Yet another pecl extension is parallel,you can install it by issuing pecl install parallel,but it has some prerequisities:
Installing ZTS(Zend Thread safety) Build PHP 7.2+ version
if you build this extension by source, you should check the php.ini like config file,then add extension=parallel.so to it
then see the full example gist :https://gist.github.com/krakjoe/0ee02b887288720d9b785c9f947f3a0a
or the php official site url:https://www.php.net/manual/en/book.parallel.php
Use native PHP (7.2+) Parallel , i.e.:
use \parallel\Runtime;
$sampleFunc = function($num, $param2, $param3) {
echo "[Start: $num]";
sleep(rand(1,3) );
echo "[end:$num]";
};
for($i = 0; $i < 11; $i++) {
\parallel\run($sampleFunc, [$param1=$i, $param2=null, $param3="blabla"] );
}
for ($i = 0; $i < 11; $i++) {
echo " <REGULAR_CODE> ";
sleep(1);
}
(BTW, you will need to go through hard path to install PHP with ZTS support, and then enable parallel. I recommend phpbrew to do that.)
I prefer exec() and gearman.
exec() is easy and no connection and less memory consuming.
gearman should need a socket connection and the worker should take some memory.
But gearman is more flexible and faster than exec(). And the most important is that it can deploy the worker in other server. If the work is time and resource consuming.
I'm using gearman in my current project.
I use PHP's pnctl - it is good as long as you know what you do. I understand you situation but I don't think it's something difficult to understand our code, we just have to be little more conscious than ever when implementing JOB queue or Parallel process.
I feel as long as you code it perfectly and make sure the flow is perfect off-course you should keep PARALLEL PROCESS in mind when you implement.
Where you could do mistakes:
Loops - should be able to handle by GLOBAL vars.
Processing some set of transactions - again as long as you define the sets proper, you should be able to get it done.
Take a look at this example - https://github.com/rakesh-sankar/Tools/blob/master/PHP/fork-parallel-process.php.
Hope it helps.
The method described in 'Easy parallel processing in PHP' is downright scary - the principle is OK - but the implementation??? As you've already pointed out the curl_multi_ fns provide a much better way of implementing this approach.
But I think those 2 ways will add pretty much overhead
Yes, you probably don't need a client and server HTTP stack for handing off the job - but unless you're working for Google, your development time is much more expensive than your hardware costs - and there are plenty of tools for managing HTTP/analysing performance - and there is a defined standard covering stuff such as status notifications and authentication.
A lot of how you implement the solution depends on the level transactional integrity you require and whether you require in-order processing.
Out of the approaches you mention I'd recommend focussing on the HTTP request method using curl_multi_ . But if you need good transactional control / in order delivery then you should definitely run a broker daemon between the source of the messages and the processing agents (there is a well written single threaded server suitable for use as a framework for the broker here). Note that the processing agents should process a single message at a time.
If you need a highly scalable solution, then take a look at a proper message queuing system such as RabbitMQ.
HTH
C.
Google search results suggested that it's possible via PHP/Java Bridge . This bridge requires Java Application Server. My host (bluehost) does not allow to run any Java Application Server. What are the alternatives in this case to make iText work? Switching a host is not viable option for me.
Thanks.
I know this is an old post, but for those still looking for an answer, may I suggest having Java do your work for you as a REST service using Jersey (JAX-RS), then just call the service from PHP. You can put your Java service on a host that does allow Java and you can call that service from PHP using cURL (or even file_get_contents if your host allows it and if you know what you're doing).
Loose coupling over RESTful services is allows you to have your Java service and your PHP app on separate hosts. Don't waste you time with a PHP/Java Bridge. It's perfectly sane to call a RESTful service with a Java back-end from PHP. This way, later if you decide, your would rather re-do the service in C# or something else, at least there will be 0 impact on the PHP app because you used a language neutral RESTful approach.
I have not had any performance issues using this approach myself, YMMV
(It's mostly a matter of design: It's usually better to "bolt" things together then "weld" them together.)
Also, Java with JAX-RS is very very simple (IMHO, but again .. YMMV)
There's always iTextSharp, a C# translation of iText. Will your host let you run C#?
It looks like you can call C# from PHP, as detailed here.
The ITEXT website specifically says JAVA and .NET, Wish they can also build the PHP or PERL version
I have used the Java Itext, Its superb!!
PDFtk is a command line tool installed on many Linux systems. It is a front end to the iText library. It may do what you need. You'd have to shell out to the command line to invoke it from PHP.