Skip to content

Express Generator and Socket.io

Posted on:July 30, 2017
3 minute read

A few months ago, I tried to start a Node project to further explore web development. It’s the LAN equivalent of a Jukebox: people go to the site, add music from Youtube or a file, and the song is put into a queue. The server has a speaker, through which it plays each song. The basic elements of this include a playlist and media buttons, which are constantly changing as people add music and change the state of the player. This is a job for sockets. I used the Express Generator to start myself off. This tool gives you 4 basic pieces:

Socket.io requires direct access to the HTTP server: otherwise, it can’t filter out socket requests from other requests (things that Express needs to route). Given that the HTTP server and Express starting were split up, this proposed a problem.

Originally, I fixed it by putting everything into one file. JavaScript is a mess, so why not solve a JavaScript problem with a mess, right? I couldn’t stand that for more than a few minutes, so off I went to learn how to pass my Socket.io object correctly. Turns out, there isn’t a fully functional, elegant way. But there is a way.

I came across a guide illustrating (what seemed like) exactly what I needed. I had to tweak it a bit. The process, in a nutshell, is:

app.use(function(req, res, next){
    res.io = io;
    next();
});
router.get('/', function(req, res, next) {
    res.io.emit("socketToMe", "users");
    res.send("respond with a resource.");
});

However, this emitting of signals is all that’s covered in the guide, and for a good reason: there’s no way to listen to a socket inside these requests. I needed that to handle events such as Play/Pause button presses.

The workaround:

function (io) {
    // Require statements
    // get and post statements
    ...
    return router;
}
var routes = require("./routes/index")(io);

So far, this seems to work for a little demonstration.