AngularJS with Server-Sent Events


I have an AngularJS app with the following controller. It worked fine with GET on regular JSON resource and manual request for updates, but I cannot make it work with Server-Sent Events. The problem I am facing is that after I receive an SSE event and set/update openListingsReport variable my view is not getting updated. I am obviously missing a very basic concept. Please help me fix this.

var rpCtrl = angular.module('rpCtrl', ['rpSvc']);

rpCtrl.controller('rpOpenListingsCtrl', ['$scope', 'rpOpenListingsSvc',
    function ($scope, rpOpenListingsSvc) {
        $scope.updating = false;

        if (typeof(EventSource) !== "undefined") {
            // Yes! Server-sent events support!
            var source = new EventSource('/listings/events');

            source.onmessage = function (event) {
                $scope.openListingsReport =;
    } else {
        // Sorry! No server-sent events support..
        alert('SSE not supported by browser.');

    $scope.update = function () {
        $scope.updateTime =;
        $scope.updating = true;

    $scope.reset = function () {
        $scope.updating = false;
Problem courtesy of: krl


The problem was in the following line:

$scope.openListingsReport =;

which should be:

$scope.openListingsReport = JSON.parse(;
Solution courtesy of: krl


Just a suggestion in case someone is using SSE with AngularJs.

If you want to use server side events ,I will suggest to use $interval service that is builtin within AngularJS instead SSE.

Below is the basic example.

<script src=""></script>

<div ng-app="myApp" ng-controller="myCtrl">

    <p>Randome no:</p>



<p>The $interval service runs a function every specified millisecond.</p>

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope, $interval, $http) {
        $scope.myWelcome = new Date().toLocaleTimeString();
        $interval(function () {
            $scope.myWelcome = $http.get("test1.php").then(function (response) {
                $scope.myWelcome =;
        }, 3000);




echo rand(0,100000);
Discussion courtesy of: Vaibs

This recipe can be found in it's original form on Stack Over Flow.