I need to start a PHP script via Apache - which will basically continue to run - it will be used for a small instant messaging widget - so it must continually run to update other users on new messages. What timeout features or other barriers should I be weary of in Apache? Will some feature of Apache not allow me to run a PHP process continually?
Based on your context (instant messaging widget), you probably do not want to keep the PHP script running because:
anything on the page after the widget will not be loaded;
you will have to flush the output, otherwise user will not be able to see anything.
There are many other ways to implement instant messaging, like AJAX. I suggest you do a search on this topic.
Update: I have recently come to the realization that there is a particular name for this type of web application model, which may be a better reference than simply stating "AJAX". It's called Comet (wikipedia link).
set_time_limit will help you.
Related
I'm quite new to the Azure interface, but I have been working with PHP for a while.
I have been asked to make a routine that executes every some time at background, whose objective is to send some marketing mail.
And I have been reading about WebJobs. I can't quite get the grasp of it, though.
For me the documentation is a bit overwhelming, to say at least. So what I want to do is understand how WebJobs work and use them to execute PHP code every some time, without needing user input.
As I have said before, I have never used Azure before and have been never asked to do such things on PHP either, at least not this complex.
There is a walkthrough of how to create a webjob on the azure docs - php is supported in webjobs. Webjobs are essentially a means for App Services to run a non-interactive process on a triggered or continuous basis. You don't have to use PHP, you can run another .exe if you like. Personally I write code in c# using the webjobs SDK and deploy those, they ease the way in which triggers, inputs and outputs are passed to/from your webjob via a nice simple binding process.
Theres a more detailed explanation here. Webjobs are hosted in your app service plan, which you can look at as a container for resources used to run and host your web sites, web apis, and web jobs.
Last copuple of things to say are 1 - that via the portal you can see the status of all your webjobs, when they triggered, what the console output was, if they succeeded or failed, etc. and 2 - Azure Functions do the same thing but in a different way - they use the webjobs api but present as a "serverless" experience instead (ie. no app service required). So if you don't want to be concerned with a web site or managing the scaling yourself, see Functions documentation
I'm attempting to build a notification system for a PHP application. Every time a booking is placed, we need a notification to appear within a specified user account type inside the application.
I'm using CodeIgniter 2 on a virtual dedicated host, so I'd have the option of requesting the installation of whatever is required to get the job done.
So far, I know that PHP has limited powers over how can trigger jQuery, in that it's limited to the web browser. I know that Node.js and Socket.io can do what I want, but how would that tie in with PHP, if at all?
I also know that a polling mechanism would be bad. I've considered a method that would send the row ID via PHP to a jQuery script within the confirmation page, which could — in theory — accomplish what I have in mind, but this would rely on the web browser of the customer, which is a bit weak.
I've spent a couple of days fumbling around this question, since I'm only just getting to grips with jQuery, while I know hardly anything about Node.js or Socket.io, or what they can and cannot do, or — as mentioned earlier — how they connect with PHP.
Any advice would be welcome.
With real time push methods server pushes data to the clients(channel subscribers) whenever there is an event occurs in the server. This method is advanced than pull method like polling etc, and this will be a live communication(ie, client gets live updates from server with no time. In pull method there is a time interval between each query).
Examples for real time push methods: Faye, pusher, socket.io, slanger
Most of the real time push methods are built on ruby or nodejs. So if you wish to setup your on real time server you must setup them in your server(probably ruby or nodejs) and you can communicate with that server from php using curl statements.
Also there are php libraries available for these operations.
If you like to setup slanger then you can use the pusher php library itself (may be you need to modify it slightly to use with slanger). And if you like to use faye then here is a php library wrote my self: faye php wrapper
You could store notifications in database, with corresponding timestamp.
Then, use long pooling to receive messages in jQuery, that calls PHP for notifications.
Cool example was given in this anwser:
How do I implement basic "Long Polling"?
I'm working on feeding the client realtime events using event-streams and HTML5 SSEs client-side.
But some of my events will actually come from form submissions by other clients.
What's the best method for detecting these form submissions (so as to append them to the event-stream script) ASAP (after they occur)?
So essentially, I need realtime cross-script messaging between multiple instances of different scripts instantiated by different clients, analagous to X-doc messaging in JS, but for PHP.
The best I can come up with is to repeatedly poll a subdir of /tmp for notification files, which is a terrible solution.
Often you can use MYSQL to play the role of the tmp dir you were talking about. This is more portable because they don't have to be on the same server to do this and the data is separate. However the scripts will have to manually check the mysql location to see if the other one has taken care of this. The other option is to open sockets and write back and forth in real time or to use some prebuilt tool for just this purpose which I'm pretty sure might exist.
If you want the events to be triggered near to realtime, then you need to handle them synchronously - which means running a daemon. And the simplest way to implement a daemon which can synchronize data across client connections is to use an event based server. There's a nice implementation of the latter using php here - there are plenty of examples of how to daemonize a PHP process on the interent. Then just open a blocking connection to the server from your PHP code / access this via comet.
How can New Relic tap into my app with a simple install? How does it know all the methods, requests, etc?
It works for RoR, PHP, etc.
Can anyone explain the technology behind it? I'm interested in tapping into my Rails app, but I want to do so smoothly like New Relic.
Thanks
First up, you will not manage to duplicate the functionality of NewRelic on your own. Ignoring the server-side, the rpm Gem is a pretty complex piece of software, doing a lot of stuff. Have a look at the source if you want to see how it hooks into the Rails system. The source is a worth a read, as it does some cool stuff in terms of threading and marshaling of the data before sending it back to their servers.
If you want a replacement because Newrelic is expensive (and rightly so, it's awesome at what it does), then have a look at the FreeRelic project on Github.
They are using ASPECT ORIENTED PROGRAMMING CONCEPTS AND Reflection heavily for Intercepting original method call and adding instrumentation around that.
In a general way, New Relic's gem inserts kinda middleware in your web framework, and collects data from your endpoint (think as a rails route) until it's response. After every "harvesting time" (defaults to 60 seconds), it sends a post request to NR services with this data.
You can also tailor data you need with Custom Metrics, Custom Events.
Is also possible to do queries with NRQL and build graphs with that (like you would do in Graphana).
They have a customize service for Wordpress too, but is a bit messy in the start.
Some options if you want to save some money is configure cloudwatch + datadog, but I would give a shot to their service if uptime is crucial for your app.
For a rails solution you could simply implement a more verbose logging level (development/debug level) and interrogate the production.log file for specific events, timings etc
For Java they are attaching a Java agent to JVM which intercepts method calls and monitor them. You can use AspectJ to replicate the same behaviour and log every method call to wherever you want, let's say create custom Cloudwatch metrics.
In case of Java it's bytecode ingestion. They "hacking" the key methods of your application server and add their code in it. Then they send relevant transaction info to their server, aggregating it and you can see the summary. It's really complicated process so I don't think one dev can implement it.
If you’re already familiar with New Relic’s application monitoring
then you probably know about New Relic’s agents that run in-process
on web apps collecting and reporting all sorts of details about whats
happening in the app. RUM leverages the agents to dynamically inject
JavaScript into pages as they are built. The injected JavaScript
collects timing information in the browser and contains details that
identify the specific app and web transaction processed on the
backend as well as how time was spent in the app for each request.
When a page completes loading in an end user’s browser, the
information is sent back to New Relic asynchronously – so it doesn’t
effect page load time.
You can turn RUM on/off via your application settings in New Relic.
As well, you can turn RUM on/off via the agent’s configuration file
(newrelic.yml – a ‘browser_monitoring auto_instrument’ flag has been
introduced).
The agents have been enhanced to automatically inject JavaScript into
the HTML pages so using RUM is as simple as checking the checkbox on
the New Relic control panel. However, if you’d prefer more control,
you can use New Relic’s Agent API to generate the JavaScript and thus
control exactly when and where the header and footer scripts are
included.
I'm trying to find a efficient way to watch the server log on a webpage, i don't mind building an app i just can't work out the best way to do it.
Is there a way to keep a stream open to a file with php and to the browser? or will it have to be done by polling the file every x seconds?
Thanks in advance,
Shadi
The best solution is definitely AJAX in some capacity. The only way to have the server "push" to you the way you describe (maintain an open stream) would require the HTTP connection to remain open which would ultimately trigger timeouts and consume a lot of resources. I would look into the Cometd library. The downside to this is that I believe it depends on Java although the site does mention perl, python and "other languages." In the worst case, you could use a specific jetty implementation just for log monitoring on a specific port. Regardless, that framework would most likely be your best bet.
Any web-based chat mechanism essentially uses a push architecture and would be good to look at for some inspiration. In this case, instead of users creating messages that are fired to other users, the server creates the events (when a log message is generated). Check out this article on Facebook chat for some insight into how they do it. Google chat might be worth looking into if you can find some stuff on the architecture.
For the actual logging, I'm not sure if you are in need of help for that, but log4php which is currently under incubation might be a good place to start as it provides you with a configuration that can simultaneously log to an arbitrary number of "loggers" like database, file, socket, etc. You could likely find one that would allow you to tie it into whatever push framework you elect to use.
Good luck!
Remember that the web model is essentially stateless (disconnected). Having that in mind when a client submits a request, the server processes the request and then send a response accordingly. You can have track of the clients action using cookies and/or sessions, but the resources reserved for a request are released after the response is submitted back.
I think that the best way to meet your goal, is to develop a web services that checks for the status of the log and fetch the diff (if any). Your app may consist of a web page with a div that will display the diff from the web service.
A script with a timer will trigger the call to the web service.
I will try to do something like this in a few weeks, and I will post the entire solution on moropo blog (spanish). You can ask for a post translation using the comments.
The best way to do it is to use AJAX to pull the file content every x seconds, giving the illusion of real time.
If you do want real time, you can use an XMPP server, but from what I can see, the first solution is far sufficient and does't require a lot of work.
Try wonlog.
https://www.npmjs.com/package/wonlog
You can stream multiple log files to a web browser.