DEV Community

Cover image for How to uncouples emit events from connection event into and express

How to uncouples emit events from connection event into and express

wakeupmh profile image Marcos Henrique ・Updated on ・2 min read

Preface πŸ‘“

I've worked in a project with node.js, express and websockets, In a part of the project I realized the need to uncouples the sending of messages to the websocket, since I would need to do this in a place where there was no instance of it, in my case it was on a specific route in the middle of the business rule logic.

I would need to send the return in real time of each stage of this processing, and among all the alternatives, such as passing the socket instance as a route by route parameter (in my project I have unification of the routes by prefix) and having to refactor a lot of the that I had already done, I found this way that I present to you πŸ“¨

Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and servers.

Socket.IO is NOT a WebSocket implementation.

Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the packet id when a message acknowledgement is needed.

That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server either.

You can see more about here.

Socket Service Class ✨

const socketIo = require('');

class SocketService {
   constructor(server) { = socketIo(server);'connection', socket => {
       console.log('user connected')

  emiter(event, body) {
    if(body), body);

module.exports = SocketService;

Implementation in app.js πŸ‘Ύ

In the main of my application, there I was put the instance of Socket Service as a singleton into express app.

With that I was able to use it on other routes without having to modify anything on them

const express = require('express');
const app = express();
const server = require('http').Server(app);

app.use(`/`, require('./routes'));

server.listen(3001, function () {
  console.debug(`listening on port 3001`);

 app.set("socketService", new SocketService(server));

Using in the route controller or anywhere you want πŸ›£

//using req to access app that I had defined before"socketService").emiter('message', req.body);

//anywhere with access to app:
app.get("socketService").emiter('message', req.body);

that's all, i hope this can help you in your projects!


Editor guide
sharpex profile image

This is great i have searched for it for two days tried all stackoverflow solutions none was working....Thanks

duanegabriel profile image
Duane Gabriel

Brother, you're article in a few words solved my problem, thank you.
Cheers from Brazil.

thebrown profile image

Thank you so much, Finally I found the way to use Socket IO in the express route

wakeupmh profile image
Marcos Henrique Author

I'm happy to have helped 🀘