Wayne Smallman
Wayne Smallman

Reputation: 1720

Send data between PHP and jQuery

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.

Upvotes: 0

Views: 153

Answers (2)

Harish Ambady
Harish Ambady

Reputation: 13121

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

Upvotes: 2

Artek Wisniewski
Artek Wisniewski

Reputation: 797

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"?

Upvotes: 0

Related Questions