Sure
There is kinda well known and popular library from @btford. You just need to include the library into your project and make a factory that returns you helpers like addListener, removeListener, removeAllListeners, emit.. for your project.
angular.module('myApp', [
'btford.socket-io',
'myApp.MyCtrl'
]).
factory('mySocket', function (socketFactory) {
return socketFactory();
});
Sample
There is also a nice webtutorial on HTML5 Rocks where is built a simple app with Node.js (Express) and Angular.js and they communicate between each other via Socket.io in both directions. Sources are available on Github.
Basics (sample from tutsplus)
Basically, just make a simple Node.js listener with callback:
var io = require('socket.io')(8080);
io.sockets.on('connection', function (socket) {
socket.on('echo', function (data) {
socket.emit('echo', data);
});
socket.on('echo-ack', function (data, callback) {
callback(data);
});
});
.. include the scripts to your frontend app ..
<script src="'angular.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="angular-socket.js"></script>
<div ng-controller="Ctrl">
<input ng-model="dataToSend">
<button ng-click="emitBasic()">Send</button>
<button ng-click="emitACK()">Send (ACK)</button>
<div>Server Response: {{ serverResponse }}</div>
<div>Server Response (ACK): {{ serverResponseACK }}</div>
</div>
.. include the module ..
var module = angular.module('socket.io', []);
app.controller('Ctrl', function Ctrl($scope, $socket) {
$socket.on('echo', function (data) {
$scope.serverResponse = data;
});
// ………
});
AngularJSis a frontend based application and very good for single page applications.node.jsis a backend based application. Interacting with socket.io is not a good way to handle a interface (keep your environments atomar). What about HTTP & RESTful?