0

Web socket.io with AdonisJs not working properly.

I'm using a lib to perform tasks inside adonis, adonis5-scheduler.

Below is my task.

import { BaseTask } from 'adonis5-scheduler/build'

export default class GetRouletteGame extends BaseTask {
    public static get schedule() {
        return '*/3 * * * * *'
    }
    /**
     * Set enable use .lock file for block run retry task
     * Lock file save to `build/tmpTaskLock`
     */
    public static get useLock() {
        return false
    }

    public async handle() {
      Ws.io.emit(`taskName`, 1)
      console.log('ok')
    }
}

Below is my Ws.

import { Server } from 'socket.io'
import AdonisServer from '@ioc:Adonis/Core/Server'

class Ws {
  public io: Server
  private booted = false

  public boot() {
    /**
     * Ignore multiple calls to the boot method
     */
    if (this.booted) {
      return
    }

    this.booted = true
    this.io = new Server(AdonisServer.instance!, {
      cors: {
        origin: '*'
      }
    })
  }
}

export default new Ws()

Calling Ws.io.emit from the controller works.

Calling Ws.io.emit from the task doesn't work.

Note for the task Ws.io.emit does not work, however the console.log displays the message.

Task does not run Ws.io.emit

1 Answer 1

0

Resolved

await fetch('http://127.0.0.1:3333/endpoint' to control

From the controller I called emit and it worked.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to implment socket.io with adonis. Tried this docs.adonisjs.com/cookbooks/socketio-with-adonisjs but no luck. What url are you using to connect to the socket server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.