Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created May 24, 2015 10:01
Show Gist options
  • Save mdmunir/01529ef854328cf26ff7 to your computer and use it in GitHub Desktop.
Save mdmunir/01529ef854328cf26ff7 to your computer and use it in GitHub Desktop.
<?php
/* @var $this \yii\web\View */
?>
<div>
Event:
<ul id="response">
</ul>
</div>
<?php
$url = \yii\helpers\Url::to(['/site/message'],true);
$js = <<<JS
var evtSource = new EventSource("$url");
evtSource.onmessage = function(e) {
$('<li>').css({color:'red'}).text(e.data).appendTo('#response');
}
evtSource.addEventListener("ping", function(e) {
var obj = JSON.parse(e.data);
$('<li>').text("ping at " + obj.time).appendTo('#response');
});
JS;
$this->registerJs($js);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment