adonis-websocket icon indicating copy to clipboard operation
adonis-websocket copied to clipboard

Broadcasting with Clustered Configuration

Open AddoSolutions opened this issue 6 years ago • 2 comments

Package version

    "@adonisjs/ace": "^5.0.8",
    "@adonisjs/auth": "^3.0.7",
    "@adonisjs/bodyparser": "^2.0.5",
    "@adonisjs/cli": "^4.0.9",
    "@adonisjs/cors": "^1.0.7",
    "@adonisjs/fold": "^4.0.9",
    "@adonisjs/framework": "^5.0.9",
    "@adonisjs/ignitor": "^2.0.8",
    "@adonisjs/lucid": "^6.1.3",
    "@adonisjs/session": "^1.0.27",
    "@adonisjs/shield": "^1.0.8",
    "@adonisjs/vow": "^1.0.17",
    "@adonisjs/websocket": "^1.0.11",
    "@adonisjs/websocket-client": "^1.0.9",

Node.js and npm version

$npm -v
6.4.1
$ node -v
v11.1.0

Sample Code (to reproduce the issue)

server.js (straight out of the textbook)

const cluster = require('cluster')

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

//console.log(cluster.worker.id)

const { Ignitor } = require('@adonisjs/ignitor')

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .wsServer()
  .fireHttpServer()
  .catch(console.error)

socket.js

Ws.channel('dispatch:*', 'DispatchController').middleware('auth')

DispatchController.js

class DispatchController {
  constructor ({ socket, request, auth }) {
    this.socket = socket
    this.request = request
    this.auth = auth
  }

  async onLocationUpdate(location){
    
    // Have tried like this
    this.socket.broadcast('updated:location',location);

    // And like this
    Ws
      .getChannel('dispatch:*')
      .topic('dispatch:3')
      .broadcast('updated:location',location);
  }

}

In the above example, I have n devices connected, and each device is broadcasting data once every 3 seconds. The information is then distributed to every other connected device. The trouble is once clustering is enabled, this ceases to work, and nobody get's each others messages.

Any ideas what I am doing wrong here, or if this is a bug?

Thanks!

AddoSolutions avatar Dec 26 '18 20:12 AddoSolutions

This is a bug. It's being fixed as I know... https://discordapp.com/channels/423256550564691970/423264181110702080/515156749813153792

hlozancic avatar Jan 12 '19 21:01 hlozancic

I realized that this problem still persists. Any alternative to get around the problem?

Brunomm avatar Mar 26 '19 19:03 Brunomm