adonisjs / transmit

A Server-Sent-Event module for AdonisJS
MIT License
65 stars 4 forks source link

Expose HttpContext in Transmit Events #3

Closed Sonny93 closed 6 months ago

Sonny93 commented 6 months ago

Package version

0.2.3

Describe the bug

For some reasons, we sometimes need to access the HttpContext in Transmit events to perform certains actions based on the current user, wich is currently not possible as we only have access to the randomly generated UUID.

Here is an example :

transmit.on('subscribe', async ({ channel, uid, auth }) => {
  const myChannel = await Channel.find(channel)
  if (myChannel) {
    transmit.broadcast(`channel/${channel}/join`, { user: auth.user })
    logger.info(`User ${auth.user.username} joined channel ${myChannel?.name}`)
  }
})
transmit.on('unsubscribe', async ({ channel, uid, auth }) => {
  const myChannel = await Channel.find(channel)
  if (myChannel) {
    transmit.broadcast(`channel/${channel}/leave`, { user: auth.user })
    logger.info(`User ${auth.user.username} leaved channel ${myChannel?.name}`)
  }
})

Reproduction repo

No response

RomainLanz commented 6 months ago

Released in 0.3.0 if you want to try and give some feedback! 👍🏻