6

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;
}
1
  • Did my answer below help you? Any comments? If it helped you then you can consider accepting the answer so other people who search for it could see that it has an accepted answer. Commented Nov 28, 2016 at 9:22

2 Answers 2

6

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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If i understand your answer correctly we can combine socket,io events && http requests to the same node-express server?
2

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)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.