Socket.io integration between and Express and Angular app is a breeze. I'll outline the Express implementation first, then outline Angular integration.
npm install socket.io and then reference in your layout.jade file:
doctype html
html(ng-app="angular_example")
head
title= title
link(rel='stylesheet', href='/bootstrap/dist/css/bootstrap.css')
link(rel='stylesheet', href='/stylesheets/style.css')
body
div(class="container-fluid")
block content
script(type='text/javascript' src='/socket.io/socket.io.js')
script(type='text/javascript' src='/angular/angular.js')
script(type='text/javascript' src='/angular-resource/angular-resource.js')
script(type='text/javascript' src='/angular-route/angular-route.js')
script(type='text/javascript' src='/javascripts/app.js')
script(type='text/javascript' src='/javascripts/controllers.js')
script(type='text/javascript' src='/javascripts/services.js')Include the following code in your app.js module (other standard Express module dependancies and middleware left out for brevity):
var express = require('express');
var http = require('http');
var io = require('socket.io');
var app = express();
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Setup custom app middleware
*/
/* setup socket.io */
io = io(server);
app.use(function(req, res, next) {
req.io = io;
next();
});
io.on('connection', function(socket) {
//log.info('socket.io connection made');
console.log('socket.io connection made');
});
server.listen('3000');You can now emit socket.io events from any route handler:
router.post('/', function(req, res, next) {
req.io.emit('some event');
//do some stuff
req.io.emit("some other event - now that we've done stuff");
});
Note for me; good article on Angular providers: https://gist.github.com/demisx/9605099