How do I configure the Server-sent events to be used under Nginx?

Source: Internet
Author: User
Using PHP+JS to implement server Push, in the WAMP environment can be effective, but transferred to the Nginx under Linux, it is not used, is the environment configuration problem? How do I configure it?
The following is the relevant code
Server-side PHP code:

date_default_timezone_set("America/New_York");header("Content-Type:text/event-stream");$userid = $_SESSION['userid'];//session_write_close();$db = get_db();while(true) {  $notices = $db->lRange("User:{$userid}:Notice", 0, -1);  $notice_num = 0;  foreach ($notices as $value) {    $readed = $db->hGet("Notice:{$value}:Info", 'readed');    if ($readed == false) {      $notice_num++;    }  }  echo "event: ping\n";    $curDate = $notice_num;    echo 'data: {"time": "' . $curDate . '"}';    echo "\n\n";    ob_flush();    flush();    sleep(30);}

Front-end JS:

var evtSource = new EventSource(serverName + 'index.php/Home/NoticePush/index');    evtSource.addEventListener("ping", function(e) {         var obj = JSON.parse(e.data);         $('div#header div#user_notice a').text(obj.time);   }, false);

Reply content:

Using PHP+JS to implement server Push, in the WAMP environment can be effective, but transferred to the Nginx under Linux, it is not used, is the environment configuration problem? How do I configure it?
The following is the relevant code
Server-side PHP code:

date_default_timezone_set("America/New_York");header("Content-Type:text/event-stream");$userid = $_SESSION['userid'];//session_write_close();$db = get_db();while(true) {  $notices = $db->lRange("User:{$userid}:Notice", 0, -1);  $notice_num = 0;  foreach ($notices as $value) {    $readed = $db->hGet("Notice:{$value}:Info", 'readed');    if ($readed == false) {      $notice_num++;    }  }  echo "event: ping\n";    $curDate = $notice_num;    echo 'data: {"time": "' . $curDate . '"}';    echo "\n\n";    ob_flush();    flush();    sleep(30);}

Front-end JS:

var evtSource = new EventSource(serverName + 'index.php/Home/NoticePush/index');    evtSource.addEventListener("ping", function(e) {         var obj = JSON.parse(e.data);         $('div#header div#user_notice a').text(obj.time);   }, false);

Because the nginx buffer problem, the default nginx is a buffer, PHP refresh output to nginx invalid.
Need to output the front and add a

header('X-Accel-Buffering: no');
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.