2

i was doing long polling , but it can see in my network chrome my long polling in status pending that means is waiting for new data and changes the database , but when I try to insert one item to my database then , but when I try to insert one value show keep that long polling in real time i cant see real time updatings .. how can I fix it?

pusher controller codeigiter

public function pusher() {
    header('Content-Type: application/json');
    $user_id = $this->session->log['id'];
    set_time_limit(0);
    while (true) {
        $firstCall = false;
        if (isset($_GET['timestamp'])) {
            $last_ajax_call = $_GET['timestamp'];
        } else {
            $last_ajax_call = time();
            $firstCall = true;
        }

        clearstatcache();
        $notificationsCount = $this->notification->checkForNotifications($last_ajax_call, $user_id);
        $newData = (int) $notificationsCount > 0 ? true : false;
        $notifications = [];
        if ($newData) {
            $dataSet = $this->notification->getNotifications($user_id, $last_ajax_call);

            foreach($dataSet as $data) {
                $notifications[] = $data;
                $finalNotificationTime = $data['timestamp'];
            }

            $result = array('notifications' => $notifications, 'timestamp' => $finalNotificationTime);
            $json = json_encode($result);
            echo $json;

            break;

        } else {
            if ($firstCall) {
                $dataSet = $this->notification->getUnreadNotifications($user_id);

                foreach($dataSet as $data) {
                    $notifications[] = $data;
                }

                $result = array('notifications' => $notifications, 'timestamp' => $last_ajax_call);
                $json = json_encode($result);
                echo $json;
                break;
            }
            sleep(1);
            session_write_close();
            continue;
        }
    }
    exit();
}

ajax

function check_notifications(timestamp) {
        var queryString = {
            'timestamp': timestamp
        };

        $.ajax({
            type: 'GET',
            url: URL_GET_NOTIFICATION,
            data: queryString,
            success: function (data) {
                // put result data into "obj"
                for (var i in data.notifications) {
                    notify(data.notifications[i].message, data.notifications[i].type, data.notifications[i].timestamp);
                }
                check_notifications(data.timestamp);
            }
        });
    }
 check_notifications();

screenshot_network

  • You don't need _pending_. Use JavaScript's `setInterval()` function to send/get data periodically. Just set it like 2000, 3000 or 5000 (to avoid keeping server too busy) for your AJAX call. Check the example [here](https://stackoverflow.com/questions/17529783/how-do-you-set-interval-to-ajax-call-in-jquery). – Tpojka Jul 22 '17 at 07:04
  • i dont need short polling , how can I do it with my own code? –  Jul 22 '17 at 09:25
  • 1
    Check something how it is done in Laravel broadcasting with pusher and laravel echo. Basically you need some system of event and listener in PHP that would observe and push notification to frontend. That would be better than blocking request which is waiting "something to happen". – Tpojka Jul 22 '17 at 20:42

0 Answers0