getChannel('').topic('') always returning null

Hi.

I’m trying to get a dynamic topic to broadcast an event but no matter what I try it’s always retuning null.

Channel register

Ws.channel('tactical:*', 'TacticalUnitWsController')
    .middleware(['auth:tactical']);

Client code

init() {
    this.ws = Ws('ws://localhost:3333');

    this.ws.withJwtToken(this._authService.token).connect();

    this.ws.on('open', () => {
      this.channel = this.ws.subscribe('tactical:main');

      this.channel.on('message', this.onMessage.bind(this));
    });
}

Trying to get the topic

const topic = Ws.getChannel('tactical:*').topic('tactical:main');
console.log({ topic });

It seems like everything I’m doing is right, the communication between the socket controller and the client is fine. But now I have to get the topic from a Http Controller to broadcast a message and I just can’t.

Is anything wrong with my code?

1 Like

Okay, so after some digging in this exact forum I found that it worked for someone after they commented this code on the server.js file.

const cluster = require('cluster')

if (cluster.isMaster) {
  for (let i = 0; i < 4; i++) {
    cluster.fork()
  }
  require('@adonisjs/websocket/clusterPubSub')()
  return
}

I commented it and it’s now working. It’s strange because the docs say that this snippet of code is necessary. I hope I didn’t break anything.

2 Likes

It’s necessary when running in cluster mode :slightly_smiling_face:

1 Like

I feel dumb for not reading it properly… Thanks for the heads up!