Inversifyjs: Using inversify-express-utils, how can i integrate websockets?

Created on 15 Nov 2016  路  18Comments  路  Source: inversify/InversifyJS

From SO:

http://stackoverflow.com/questions/40612963/using-inversify-express-utils-how-can-i-integrate-websockets

cc: @codyjs this is not supported right? would it be hard to implement?

Most helpful comment

yay, working. great! will add it to the documentation! thanks a lot.

All 18 comments

I think this should be currently supported, try this:

EDIT: I misread the docs. You need to wrap the express app in an http.Server.

// ...

// server.build() actually returns an Express.Application instance
let app = server.build();
app.listen(3000, 'localhost', () => console.log('listening on http://localhost:3000'));

let server = require('http').createServer(app); 

// socket.io expects an http.Server versus a InversifyExpressServer
// see http://socket.io/docs/#using-with-express-3/4
let io = socketIo(server);

// ...

@codyjs thanks a lot for helping out :heart:

No problem, hopefully that does actually work!

Hi @dkellenb can you confirm that this solves your problem? Thanks!

Thanks for your hint. Tried it out, but it's still not possible to connect. Server does not answer. By the way, you should not declare server variable twice.

// server.build() actually returns an Express.Application instance
let app = server.build();

// socket.io expects an http.Server versus a InversifyExpressServer
// see http://socket.io/docs/#using-with-express-3/4
let httpServer = require('http').createServer(app);
let io = socketIo(httpServer);
io.on('connection', (client) => {
  console.log('Client connection opened');

  // Success!  Now listen to messages to be received
  client.on('message', (event) => console.log('Received message from client!', event));
  client.on('disconnect', (event) => console.log('Client has disconnected'));

});

No luck so far.

I've added a demo project setup, where you can reproduce the behavior:
=> https://github.com/dkellenb/inversify-express-utils-websockets

  1. npm install
  2. npm install ts-node -g
  3. ts-node src/bootstrap.ts

Using https://chrome.google.com/webstore/detail/dark-websocket-terminal/dmogdjmcpfaibncngoolgljgocdabhke following steps can be done to verify the behavior

17:26:09    command:    /connect ws://localhost:3000/
17:26:09    system: Connecting to ws://localhost:3000/
No protocol negotiation.
17:26:09    error:  WebSocket error.
17:26:09    system: Connection closed.
Close status: 1006 (Abnormal Closure)

@dkellenb My guess would be on changing app.listen(3000) to httpServer.listen(3000) as you have wrapped the Express application in a HttpServer.

Tried your suggestion on https://github.com/dkellenb/inversify-express-utils-websockets/. HTTP working, but no success with WebSocket. Still the same WebSocket error.

21:32:29    command:    /connect ws://localhost:3000/
21:32:29    system: Connecting to ws://localhost:3000/
No protocol negotiation.
21:32:29    error:  WebSocket error.
21:32:29    system: Connection closed.
Close status: 1006 (Abnormal Closure)

Your connection url should be ws://localhost:3000/socket.io/?EIO=3&transport=websocket

yay, working. great! will add it to the documentation! thanks a lot.

Can one of you guys @codyjs @goenning @dkellenb please document the answer on SO to ensure that it will help other users in the future? Thanks a lot for your help!

@remojansen : Done, documented also on SO.

Great! But in general socket.io is used to emit something in a controller or somewhere else. @remojansen is there a way to make socket.io injectable? It is also will be nice if we can make controller-style injection to handle io messages.

@FriOne I'm not experienced with Sockets but if it is an object you should be able to inject it using:

container.bind<SocketType>("SocketType").toConstantValue(socketInstance);

// and in your controllers

@inject("SocketType")

@remojansen socket.io needs server instance, so it can be injected only after port listening. Like here. I tried to make binding after, but controller saying No matching bindings found

What if you take the variable created in Line 36, and put this into a container?

@lholznagel you can't do it after building this. But you should build because you need server instance for socket.io.

Hi @FriOne, you find any solution for your question? I'm have the same problem.

Was this page helpful?
0 / 5 - 0 ratings