Express.js with websockets

My application is currently based on Expressjs + angularjs. I want to start some two-way calls along with existing HTTP calls. I looked at some web chat tutorials, but not one of them is integrated with expressjs.

Start a network connection on a new port? How to integrate my corner objects using websocket?

Can I just create some routes and controller functions, and some of them work differently?

+6
source share
1 answer

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

eg. in my project, I am doing 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 is also an example with an expression on the Socket.IO wiki.

+4
source

Source: https://habr.com/ru/post/971455/


All Articles