In nginx, how does one configure Server-SentEvents?

Source: Internet
Author: User
Using php + js to implement server pushing can take effect in the wamp environment, but it cannot be used if it is transferred to nginx in Linux. Is it an environment configuration problem? How do I configure it? The following code is the php code on the server: {code ...} front-end js: {code ...} using php + js to implement server pushing can take effect in the wamp environment, but it cannot be used if it is transferred to nginx in Linux. Is it an environment configuration problem? How do I configure it?
The following code is used:
Php code on the server:

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);         $('p#header p#user_notice a').text(obj.time);   }, false);

Reply content:

Using php + js to implement server pushing can take effect in the wamp environment, but it cannot be used if it is transferred to nginx in Linux. Is it an environment configuration problem? How do I configure it?
The following code is used:
Php code on the server:

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);         $('p#header p#user_notice a').text(obj.time);   }, false);

Due to nginx buffer problems, nginx has a buffer by default, and the php refresh output is invalid for nginx.
Add one before output

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.