简体   繁体   中英

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!

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.

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);
                });
            };
    });
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM