Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Socket.io combined with Express.JS (using Express application generator)

I'm trying to use Socket.io combined with Express.JS (using Express application generator).
I've found some aswers how to do this (Using socket.io in Express 4 and express-generator's /bin/www).
My problem is that i cannot make use of the sockets inside the routes folder. I can use them in the app.js and bin/www.js files. When i call the route index.js it just keeps loading the webpage for a long time without giving any errors.

bin/www.js

...
/**
 * Create HTTP server.
 */

var server = http.createServer(app);

var io     = app.io
io.attach( server );
...

app.js

...
// Express
var app = express();

// Socket.io
var io = socket_io();
app.io = io;
var routes = require('./routes/index')(io);
...

routes/index.js

module.exports = function(io) {
    var app = require('express');
    var router = app.Router();

    io.on('connection', function(socket) {
        console.log('User connected');
    });

    return router;
}
like image 952
André Freitas Avatar asked Mar 10 '23 14:03

André Freitas


2 Answers

Here is a simple example on how to use Socket.io with Express that I made available on GitHub here:

  • https://github.com/rsp/node-websocket-vs-socket.io

The backend code is this:

var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
  console.error('express connection');
  res.sendFile(path.join(__dirname, 'si.html'));
});
io.on('connection', s => {
  console.error('socket.io connection');
  for (var t = 0; t < 3; t++)
    setTimeout(() => s.emit('message', 'message from server'), 1000*t);
});
http.listen(3002, () => console.error('listening on http://localhost:3002/'));
console.error('socket.io example');

See https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js

As you can see here, I am creating the express app with:

var app = require('express')();

Then I create an http server with that app with:

var http = require('http').Server(app);

And finally I use that http server to create the Socket.io instance:

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

After running:

http.listen(3002, () => console.error('listening on http://localhost:3002/'));

it all works together.

You can see the entire example on GitHub with both backend and frontend code that works. It currently uses Express 4.14.0 and socket.io 1.4.8.

like image 77
rsp Avatar answered Apr 13 '23 00:04

rsp


For anyone who still want to use socket.io and express http request. Easiest way is to create two seprate instance of http server listing to different ports. 1 for websockets and 2nd for api requests.

const express = require("express");
const app = express();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer, {
    path: '/'
});

// routes and io on connection

httpServer.listen(5000, () => {
   console.log("Websocket started at port ", 5000)
});

app.listen(3000, () =>{
   console.log("Http server listening at", 3000)
})
like image 30
Khacho Avatar answered Apr 12 '23 23:04

Khacho