Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js with websockets

Currently my application based on Expressjs + angularjs. I want to start few 2 way calls along with existing http calls. I went through few websocket chat tutorials but nonew of them integrated with expressjs.

Do I start websocket connection on new port? How do I integrate my angularjs with websocket?

Can I just create few more routes and controller functions and have some of them work 2 way?

like image 559
raju Avatar asked Jun 28 '14 10:06

raju


Video Answer


1 Answers

Nothing special is needed, you can use the same port for Socket.IO and express.

e.g. in my project I do something like this:

var express = require('express');
var io = require('socket.io');

var app = express();

var server = http.createServer(app).listen(SERVER_PORT, function() {
    console.log('Express server listening on port ' + SERVER_PORT);
});

// let socket.IO listen on the server
io = io.listen(server);

io.on('connection', function(socket) { /* handle connection */ });

AFAIK there is also an example with express on the Socket.IO wiki.

like image 182
kleinph Avatar answered Sep 28 '22 16:09

kleinph