ambelovsky / koa-socket-2

Socket.IO for Koa
https://www.npmjs.com/package/koa-socket-2
98 stars 14 forks source link

Namespaced socket connections don't run middleware #9

Closed RavenHursT closed 6 years ago

RavenHursT commented 6 years ago

I can't seem to get middleware to work when using namespaced connections.

//server
import IO from 'koa-socket-2'
const chat = new IO('chat')

chat.on(`connection`, ({socket}) => {
  socket.on(`someEvent`, data => {
    console.log(`captured someEvent!`, data)
  })
})

chat.attach(koaApp)
chat.use(async (ctx, next) => {
  console.log(`socket ctx => `, ctx)
  await next()
})

//client
import io from 'socket.io-client'

const socket = io('/chat')

socket.on(`connect`, () => {
  socket.emit(`someEvent`, `foo`)
})

I never see the "socket ctx =>" log when I do this, but my socket.on('someEvent') does, indeed fire. So I know the event is getting handled.. but the middleware never fires.

I also noticed that this feature is used on the unnamespaced connection (io.use()...), but not on the namespaced connection (chat.use()...), in the example (https://github.com/ambelovsky/koa-socket-2/blob/master/example/server.js#L88). So maybe there's a bug?

RavenHursT commented 6 years ago

odd.. I cloned this project directly, modified the example to have a middleware on the chat namespace.. and it worked fine.. must be something to do w/ my code.. :-/