Websocket Broadcast from ACE Command


#1

I checked this example Broadcast from anywhere but i did not get it to work.

This worked from a normal http Controller.

But if i try to use this in a Command Class (App->Command->MyClass.js)
It does’nt work.

Problem is, that broadcast is every time null!

Does anyone have a tip for me what I’m doing wrong?

Here is my Class:

const _ = require('lodash')
const Ws = use('Ws')


class MyClass extends Command {
  static get signature () {
    return `
    MyClass functions
    { --test: test }
    `
  }

  static get description () {
    return 'MyClass functions'
  }

  async handle (args, options) {
    console.time('testTimer')
    this.info(`  ${this.icon('info')} Command: MyClass...`)

    let value = _.keys(_.pickBy(options, _.isBoolean))
    switch (value[0]) {
      // test arg
      case 'test':
          this.info(`${this.icon('info')} Command with flag: --test`)

          Ws.getChannel('chat').topic('chat').broadcast('message', {
            username: ' - Servernachricht',
            text: 'Auswertung startet'
          })

          break
      default:
          this.info(`  ${this.icon('info')} Command with wrong options.`)
      break
      
    } // of switch
    this.info('----------- ende / finish -----------')

    console.timeEnd('testTimer')
  } // of handle
} // of class


#2

same issue but I found it work in http controller.


#3

From my understanding, the ace command (so as cronjob if you are going to implement) will start and execute in a new node process when you run the command which not in your application process. There are two processes and memory are not shared.
So the socket remain in your application process and your command will not reach them.

My suggestion is, you write a listener in your application for the command to trigger the broadcast and your ace command act as the client to connect to the application process like end user and trigger the event.

hope its help :grinning: