mattstyles / koa-socket

Sugar for connecting socket.io to a koa instance
242 stars 50 forks source link

Allow passing options to socket.io #8

Closed vjpr closed 8 years ago

vjpr commented 8 years ago

I need to pass an option to socket.io (engine.io) like so app.io = socketIO(app.server, {destroyUpgrade: false}). This is currently not possible. The old io.set api is deprecated too.

mattstyles commented 8 years ago

This commit allows passing options through to the socket.io instance that is being created when attaching to the app, e.g.

const socket = new IO({
  ioOptions: {
    destroyUpgrade: false
  }
})

This should work fine even when attaching only namespaces, I'm writing some tests now.

Alternatively, creating the socket.io instance inside koa-socket is only a convenience, it can be created manually and attached to the app yourself:

const app = new Koa()
const server = http.createServer( app.callback() )
const io = new socketIO( server, {
  destroyUpgrade: false
})

app._io = io

const chat = new IO( 'chat' )

chat.attach( app )
io.listen()

Note that manually creating the socket.io server yourself means that you must use namespaces, default namespace will not work.

I think the whole server creation stuff needs a little work, it could be much cleaner.