Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Angular with socket.io

I know that Angular provides awesome two way data binding on the client side, but I want more!

I'm looking for the right way to hook up angular with my server side (SailsJS which includes socket.io).

Thanks!

like image 622
danba Avatar asked Feb 17 '14 21:02

danba


People also ask

Can I use Socket.IO in angular?

After creating the Angular app, we need to install the Socket. IO-Client package which will help us communicate between our front-end and our server.

Should I use Socket.IO or WebSockets?

First of all, every modern browser supports WebSockets these days. Socket.IO uses much more boilerplate code and resources to make it fall back to other technologies. Most of the time, you don't need this level of support. Even in terms of network traffic, Socket.IO is way more expensive.

How can I send data and get data via angular to Socket server?

You can use the nodeJs eventEmitter API. So you can emit an event by eventEmitter when someone hits your endpoint(GET request) and listen that event inside your socket server and vice-versa. Save this answer.

Is Socket.IO scalable?

"The Socket.IO server currently has some problems with scaling up to more than 10K simultaneous client connections when using multiple processes and the Redis store, and the client has some issues that can cause it to open multiple connections to the same server, or not know that its connection has been severed."


2 Answers

Have you tried angular-sails-bind? (https://github.com/diegopamio/angular-sails-bind) I bet you haven't, as I've just released to the world a couple of minutes ago :). I made it for my own project and then decided to put it as a separated library so everybody could benefit and I could have my first experience developing a bower package.

I hope it could help you.

BTW: it works with sails 0.10 (as some things, like topic names had changed since 0.9). If you need to make it work with 0.9, just let me know and I'll happy to help.

like image 88
Diego Pamio Avatar answered Dec 02 '22 05:12

Diego Pamio


I would recommend trying out https://github.com/btford/angular-socket-io that allows to simply use socket object in your controllers like that:

var socketApp = angular.module('socketApp', [
    'btford.socket-io'
]);

socketApp
    .controller('messageListController', ['$scope', 'socket', function($scope, socket) {
        $scope.messages = [];

        $scope.postMessage = function(message) {};

        socket.on('connect', function () {

            $scope.$on('socket:update', function(event, data) {
                $scope.messages.push(data);
            });

            $scope.postMessage = function(message, callback) {
                socket.emit('post', message, function(commitedMessage) {
                    $scope.messages.push(commitedMessage);
                    callback(commitedMessage);
                });
            };
    });
}]);
like image 29
kasuparu Avatar answered Dec 02 '22 04:12

kasuparu