socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Cannot use socket object in controller

Open XidongHuang opened this issue 9 years ago • 1 comments

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

HI, I am new in socket.io, and I am trying to push specific data to different client-end base on their information in database. I did a very simple practice with socket.io:

//app.js
const Koa = require(‘koa');
const router = require('koa-router')();
const server = require('koa-static');
const app = new Koa();
const http = require('http').createServer(app.callback());
const io = require('socket.io')(http);

app.use(server(__dirname + '/'));

app.use(require('./controller')());

io.on('connection', (socket) => {
  
  //each client just take its specific information
  socket.on('messge', (msg) => {
      io.to(socket.id).emit('message', socket.id);
  });

})

app.io = io; //trying to let controller use 'io' by 'app.io'

http.listen(9000);

The code above works well, at least those clients side just get their socket.id and do not emit to others. But all the main data handlings are in controller, so I use app.io=io to pass socket.io to controllers. When I use io in controller:

//a_controller.js
let fnTestSocketIO = async(ctx, next) => {
    let socketIO = ctx.app.io;
    
    //just use like in the app.js, but process doesn't go in this part
    socketIO.on('message', (socket) => {
        console.log('id', socket.id);
    });
    ctx.response.body = "response from 'test socket.io'";

}
...
module.exports = {
    "GET /test/socket-io": fnTestSocketIO,
}


Unfortunately, socketIO.on(...) does not work.

Steps to reproduce (if the current behaviour is a bug)

Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.

Expected behaviour

  1. yarn start
  2. io in app.js can emit message if the client emit message.
  3. But no things show from the io when I send request in the api /test/socket-io. (It can response, but not go thorugh socketIO.on(...))

Setup

  • OS: Ubuntu 14.04
  • browser: Google Chrome 58.0.3029.81
  • socket.io version: 1.7.3

Other information (e.g. stacktraces, related issues, suggestions how to fix)

I can use:

//a_controller.js

let fnTestSocketIO = async(ctx, next) => {
    let socketIO = ctx.app.io;
    
    socketIO.emit('message', 'something');    

    ctx.response.body = "response from 'test socket.io'";

}

to emit, but it will send data to all clients. I think I need socket ids to send data, so I tried:

//a_controller.js
let sid = null;
for(let id in socketIO.sockets.sockets) {
    sid = id;
}

to get socket's id, however, it is a bad practice obviously.

Are there any hits for this? Thank you so much.

XidongHuang avatar Apr 20 '17 20:04 XidongHuang

Please ask that kind of question on Stackoverflow. Thanks!

darrachequesne avatar Apr 22 '17 05:04 darrachequesne