How to Emit from Http controller?


#1

Hi @virk
On Legacy version I can make socket emit from http controller like this

const chatChannel = Ws.channel('chat')
chatChannel.inRoom('lobby').emit('message', 'Hello world')

Does the new version support emit from http controller?
Thanks for your hard work!


#2

I search too this…


#3

I believe it’s in the docs


#4

In Adonis v4.1? No I don’t think…

Just this:

const Ws = use('Ws')

class UserController {
  async register () {
    Ws
      .getChannel('subscriptions')
      .topic('subscriptions')
      .broadcast('new:user')
  }
}

But I have this error:

TypeError: Cannot read property ‘broadcast’ of null


#5

Yup that’s the code, what else you looking for?

Regarding the null thing. If there are no subscriptions, then this returns null, so you’ll have to handle this


#6

I had this problem and corrected it as follows:

const subscriptions = Ws.getChannel('subscriptions').topic('subscriptions')

if (subscriptions) {
  subscriptions.broadcast('new:user)
}