Ajax Push System

Ajax push system

You can achieve push within PHP but it won't be the most efficient solution because to achieve push you need to maintain long running connections between your client and your server (HTTP or WebSocket connections).

See:

  • Long Polling/HTTP Streaming General Questions
  • phpwebsocket
  • php-websocket on github
  • Ratchet
  • how to implement comet in PHP - frequently linked to guide

General best practice when building a realtime infrastructure has been to decouple the push solution from your web application (Note: node.js and socket.io has changed this a bit, but personally I still think it should be decoupled). But, assuming that the latter is still the best solution you would need to write/host/install this push solution. Decoupling also means that the technology doesn't have to be PHP but you can access/use it from PHP. Maybe a bit of overkill? And especially if you don't have too many users on your site?

For simplicity I would recommend looking at using a 3rd party hosted service. I work for one such company called Pusher. Using a service such as ours lets you remove the need to install and maintain the realtime part of your application. It also makes it really easy to add the push functionality you are looking for. All you need to do is add a few lines of PHP code to your existing app to trigger the push notifications and add a few lines of JavaScript to your front-end.

Resources:

  • Most commonly use PHP library for this: https://github.com/pusher/pusher-php-server
  • Quickstart guide

If you'd like to investigate the alternatives or some of the technologies I've mentioned above I'm maintaining a list of realtime technologies which you might also be interested in.

PHP - AJAX jQuery Server Push System

You cannot push data to a browser, but what you can do is set up your webpage to poll your server every few seconds for updates. An example setup would be:

From within your website, have a javascript function that runs on a timer every few seconds (or whatever interval works best for your situation).

Start that timer on page load.

That javascript function invokes an AJAX call to a web service on your web server (more on that in a second).

On the server side you'll need some sort of system that tracks these events and stores them somewhere such as in a database table with a timestamp. So for example when XYZ creates an account, that would be logged in this "event" table in the db.

The web service called by the AJAX call will then run a query on that table and retrieve all entries since the last time it was called. Then just update the webpage with those results.

It's obviously not 100% "live" as there will be a small delay depending on what time interval you set in the JS timer but it's pretty close.

Push notifications with ajax

I can't tell you a specific system. But currently the best solution I think is to use HTML5 websockets. But there are also javascript libraries that support "Ajax Push" or "Comet", like for example JQuery. I think that should point you on the right track.

Is it possible to build a push notification system with just jquery/ajax?

Following on Joey Chong's answer, you can use SSE (Servent-Sent Events) to push messages to a user (Faye is based on websocket, that may be a problem with some proxies and firewalls, and it looks like you don't really need a bidirectional connection).

For more information on websockets and SSE : here and here.

If you don't want to build your SSE server or just want to test some client-side code, StreamData will allow you to turn any JSON API into SSE pushes (disclaimer: I represent this firm). It's free up to one million messages, so you can try and leave it if SSE is not what you are looking for.

ajax push server

After much research last night, I found this as the answer for the server side.
Then write a php script for the client side.
Then have ajax call the client which calls the server.
Only problem is some of the extensions have to be manually installed.

http://php-mag.net/itr/online_artikel/psecom,id,484,nodeid,114.html

How to used each and push data to array in jquery

You are pushing the same value over and over, the one you set here

$(document).on('change', '.slot_id', function(){
cekslotid = $(this).val();
});

Here is a working method

let data = $('.slot_id').map(function() { return this.value }).get();


Related Topics



Leave a reply



Submit