I have an existing app written in PHP (using Kohana framework) and I want to do long polling. From some things I read it seems that doing long polling with PHP is not advisable and using something like nodejs is a better choice. My question is what's the best way to integrate nodejs (or some other well suited tool for long polling) with an existing application?
For clarification my app basically is a browser plugin that you can use to send data to groups of other people. When that data is sent, I want the recipients, if they are online and also have the browser plugin, to instantly receive that data and be notified.
Possibly the best way is to let node.js listen to a port and to let PHP send messages to that port.
In Node.js you can just open a socket for listening and in PHP you can use cURL to send messages. The messages can be in JSON-format.
If the Node.js-part receives a message, it can forward it, possibly after some processing, directly to the long-polling browser.
I am creating a small hack that would allow you to do this with ease. It is in a very early stage but it has enough code for it to work: https://github.com/josebalius/NodePHP
I plan on updating the readme later today.
Related
I'm creating a mobile app with a server backend that will authenticate a user and continuously send them updates whilst listening for post data from the mobile app. These updates will be specific to the person, pulled from a database.
From my research it seems that I should use a websocket. I'm familiar with PHP so have tried Ratchet. I've created a simple chat script with Ratchet which queries a database onMessage and sends the data to the client.
My question is, are websockets right for this? When a server receives a connection it must query the db every 5 seconds and send updated info to the app. It must listen for messages that will change the db query. Everything in Ratchet's docs seems to be focussed on subscriptions to topics rather than treating each client individually, although I've gotten around this by using:
$client = $this->clients[$from->resourceId];
$client->send("whatever_message"):
Am I complicating things by using Ratchet? Or should I use a child process to handle each client?
I am sorry for a vague questions. I've researched as best I can but cannot establish whether I'm heading in the wrong direction! Thank you for any help.
That is a good formula. Sending post data from the apps while maintaining a socket connection is a good distribution of processes. However PHP might not be your best option for running the socket server.
The reason for this is PHP is a single threaded language which doesn't sport an elegant event system.
Take NodeJs as an alternative. It too is single threaded, however you can register events on socket servers allowing the software to run additional control processes while it waits for network activity.
This does not limit you to javascript however. Work can still be delegated to PHP processes from the NodeJs application (I use NodeJs as an example only, there are other options such as Java, Python, or good ol' native).
For moving work to PHP, you can either execute commands, or use a job server to enable synchronous and asynchronous tasks.
Here are a few resources you can combine to accomplish this:
http://nodejs.org/
http://socket.io/
http://gearman.org/
http://php.net/manual/en/book.gearman.php
And if you are using Symfony:
https://github.com/mmoreram/GearmanBundle
I'm trying to find an easy way to implement push notification between PHP and Flash (AS3).
The flow is as following : i get a message to my external API -> i need to update my client regarding the changes he need to do without do pulling.
I know SmartFox server has the ability to do that but SmartFox is java based, but don't understand if i can connect to smart fox from php, and i can how would i do that.
If anyone can help, please do.
SmartFox Docs
I think that a SmartFox installation might be overkill for a notifications system alone. The SmartFox server maintains a socket connection with the SWF client, and pushes+pulls data that way, so a simpler solution would be to run a PHP socket server, with an AS3 socket client implementation.
There are a few examples of each end of this setup around the web, and some that provide a sample PHP server and AS3 client in combination, such as this one: http://www.kilometer0.com/blog/code/php-xml-socket-server/
Personally, depending on the amount of likely traffic and the degree of responsiveness needed, I might prefer to poll a regular PHP script from the client on an interval of a few seconds, and perhaps reduce these if there's been no user input for a while. That's certainly easier, but you specifically asked for no pulling, so feel free to disregard that!
Possibly other questions similar to this one have been asked, but I think this would be something different to those that have been asked so far.
Can we create a chat app especially in PHP/MySQL/jQuery/Apache without continuous requests to server i.e. apache for a new messages if there are any? I don't want to use Comet or NodeJS, just want to know if it is possible to have a chat application that uses only AJAX to request Apache for new message using PHP.
By the way, if the answer is no, then what is the best way to have a perfect chat application in combination with PHP/MySQL/jQuery/Apache only? I think there must be some chat applications exist.
I am curious as to why you want to avoid all the technologies and methods developed specifically for these types of use cases but web sockets are the only other way to do it if you want to avoid comet.
Your chat application needs to connect to the server the read the incoming chat messages.
You have two choices:
Polling
This is continuously firing a new request off to the server at regular intervals to check for the messages. this is the typical AJAX style.
Web Sockets
Web sockets open one long running connection to the server. For a chat application if you are only interested in the newer browsers this is the way to go. As a bit of random information. Stack Overflow uses Web Sockets to check for any notifications.
How to implement a real fast web chat with PHP?
Has anybody ever wonder why Facebook chat is just so really really fast? even in IE without WebSocket.
Isn't the only way is to setInterval in JS to check for new messages? But I feel it (Facebook chat box) like having instant reaction.
How to implement such great thing with PHP?
The instantaneous chat you're describing is generally acheived by a something called "Long Polling" or, if we're talking about AJAX, "Comet" (Wikipedia talks about it). Polling tends to strain Apache servers, but there are some specialized servers to deal with it like APE. I'm not sure but I think you can do the same with NodeJS and NGINX handles the stress pretty well.
Here's an article here about how to implement a long polling chat with PHP jQuery and AJAX.
Best of luck, and I hope it helped!
I agree with #joseph-szymborski although it would make sense to start looking at WebSocket solutions which fallback to WebSockets via Flash and/or long-polling.
Here are some relevant SO questions:
How to implement facebook like notification on cakephp? - PHP/jQuery
ajax Push system - PHP/Ajax
Apache with Comet Support - the question itself is very good.
PHP Jquery: chat system, what is the Ideal framework for this? - relevant to your question.
If you want to work with PHP or are on shared hosting then I'd recommend looking at a hosted realtime web solution.
You might want to consider Node.js to serve the clients in 'real time' since Long polling with PHP/AJAX may cause strain on your server.
But the implementation itself is an uphill task. Just saying.
Long polling with PHP/AJAX may cause strain on your server.
My typical theoretical implementation of the same:
Create a Node.js server to query a database.
Send variables and/or session data from php to Node js using cURL.
Parse the url in your Node.js server and use the variables to check
for changes in database.
Emit new data if changes occur and send to client.
How to make php client to client, like the chat way ? One client connects and sends something to the other client and only he recieve not all clients.
Your Question?
If I understand correctly you want one-to-one(private) messaging.
Socket Programming using PHP
You need to learn Socket programming with PHP. You could start by studying this tutorial. This has scaling problems written all over it, because PHP does not have non blocking IO, proper thread model. I advise you to just use it for fun little projects.
Non blocking IO using PHP
You could try and use PHP-MIO. I have not yet tried this, but I guess it might scale. But then again from Apache(PHP) side you will have the same problems. But when using this from both sides it could work...
long-polling(blocking IO) using PHP
P.S: got bored so I have not completely tested this ;)
Download
Below I present two solutions(prototypes) which do NOT scale. One solution uses Redis pubsub. For this you need to install(compile) redis. For this you a POSIX OS is desired, although some people have ported it to Windows. You can also use the free redistogo.com instance. The Redis solution is the prefered solution. I have put everything in an archive which you can download from here.
I also give a solution which uses named pipes. This solution does not require you to use Redis, but instead this approach needs access to the file system. I believe that this approach should also work on Windows(You have to change the filename to windows-style). I would like for somebody to try this out :). I can not test this anymore, because I have completely switched to POSIX OS(Ubuntu) a long time ago.
Requirements
At least PHP 5.3 and preferable a POSIX OS, redis.
How to use
To use both solutions you need to open two browsers(Browser A/B). I assume you are using localhost for development and that you can access files from http://localhost/6646733.
point browser A to http://localhost/6646733/redis?me=somebodyelse&to=alfred you should replace redis with pipe when trying out named pipes.
Point browser B to http://localhost/6646733/redis?me=alfred&to=somebodyelse
In browser A type a message into the textarea, which will be sent to browser B.
In browser B read the message just sent from browser A
Solutions not using PHP
The solutions below scale.
Pusher(Hosted)
With for example the hosted solution Pusher you can do chat/messaging without the scaling nightmare. Pusher even is generous to provide a free plan. But be aware that the cheap plans do NOT offer SSL so the messages can be intercepted. You should never sent private information over the wire, when not using SSL. Users/developers have provided a nice little library to use Pusher from PHP. The problem with this approach is that you are not in control, but pusher is, but then again you don't have to worry about any details.
Socket.io(open-source)
I really like socket.io, but there are off course a lot of other solutions like for example tornado. You could use Redis to efficiently communicate between PHP and the other solution(socket.io).
I don't fully understand what you are trying to do, but you can use some kind of database and have it store messages that is sent to each user, and then have your client page refresh the chat part with a AJAX kind of query to update the chat. It will then behave simular to the new Facebook chat, where all messages are stored even the ones sent in normal chat and mail. So clients can mail and chat each other at all times, when they are online it will show in their chat and when thy are offline it will show up in their inbox. But this might not be what you are trying to do.
For implementing best Chat application, use jabber server and write clients using js/flex
http://en.wikipedia.org/wiki/Extensible_Messaging_and_Presence_Protocol
If it's not like chat but you want to send messages over without a server, you need both clients be server as well. A server will listen on a port for incomming connections. Write a daemon that spawns a new thread each time a client connects. Within this thread you handle the messaging.
Client A opens a connection to the server (Client B) and they can talk to each other. Or you let Client A become server and let Client B connect.