angularjs with socket.io

668 views
Skip to first unread message

Jakub Arnold

unread,
Aug 8, 2012, 10:53:28 AM8/8/12
to ang...@googlegroups.com
I have a socket.io + backbone.js chat application and I would like to re-implement it with angular.

The problem is, that I don't really see how the async messaging model that socket.io uses. I can't really inject the socket object, since it has callbacks hooked all over it. The only thing I can think of is using event emitters, but how do I target a specific controller?

The only thing that comes to my mind is broadcasting form the root scope, but that doesn't really seem like a good practice.

Efflam Daniel

unread,
Aug 8, 2012, 11:25:46 AM8/8/12
to ang...@googlegroups.com

Ricardo Bin

unread,
Aug 8, 2012, 11:28:10 AM8/8/12
to ang...@googlegroups.com
I built and app using socketIO with angular few weeks ago.

My first thought was using event emitters, but i decided use it as service (with factory method), with intermediate methods calling native socket methods.

I inject it and call my built methods.

Works fine. =)

disperse....@gmail.com

unread,
Aug 8, 2012, 12:15:35 PM8/8/12
to ang...@googlegroups.com
I don't know about socket.io specifically but am using Websockets and it works great.  Here is a code fragment for implementing the Websocket service:

angular.module('myApp.services', [], function($provide) {
    $provide.factory('websocketFactory', function($window) {
        var wsClass;

        if ('WebSocket' in $window)
        {
            wsClass = WebSocket;
        }
        else if ('MozWebSocket' in $window)
        {
            wsClass = MozWebSocket;
        }

        return wsClass
            ? function(url) { return new wsClass(url); }
            : undefined;
    });
}

Andy Joslin

unread,
Aug 8, 2012, 2:08:41 PM8/8/12
to ang...@googlegroups.com
Here's my socketIo service.  It just has an $on method that does $apply, some logging helpers, and everything else is normal. http://d.pr/n/ies4

Ricardo Bin

unread,
Aug 8, 2012, 3:25:36 PM8/8/12
to ang...@googlegroups.com
Mine is almost equal =D

I added a clean method in my service that call socket.removeAllListeners too. 

Witold Szczerba

unread,
Aug 9, 2012, 6:38:41 AM8/9/12
to ang...@googlegroups.com
Hi,
you should never do thing like this:

function someOutOfAngularCallback() {
//tell something to angular
//invoke some controller's or service's methods
//and at the end:
$rootScope.$apply();
}


The above should look like this to confirm with AngularJS specification:

function someOutOfAngularCallback() {

//here you can do stuff not related to angular
//and now:

$rootScope.$apply(function() {
//we are now withing Angular call stack,
//do here whatever you want with
//angular controllers, services or whatever
}
}

Regards,
Witold Szczerba

On 8 August 2012 20:08, Andy Joslin <andyt...@gmail.com> wrote:
> Here's my socketIo service. It just has an $on method that does $apply,
> some logging helpers, and everything else is normal. http://d.pr/n/ies4
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>
Reply all
Reply to author
Forward
0 new messages