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

Use Websockets in Tasks

Open vaneetkjoshi opened this issue 6 years ago • 4 comments

Please explain how to use adonis WS ( websockets ) to push data using this scheduler i got this error Ws .getChannel('channel:*') .topic('channel:XYZ') .broadcast('message', 'Message for socket')

TypeError: Cannot read property 'broadcast' of null at curl.setHeaders.get.then

vaneetkjoshi avatar May 28 '18 16:05 vaneetkjoshi

Connections must be made from a web process since the scheduler process does not run a web server.

I suggest using something like Redis to send a message from the scheduler process(es) to the web process(es). Then, the web process can send a websocket message to the client.

https://adonisjs.com/docs/4.1/redis#_pub_sub

nrempel avatar Jun 02 '18 17:06 nrempel

You can try use AdonisJS Events for send event to WS. I use it in adonis version 3.2

ntvsx193 avatar Jul 08 '18 10:07 ntvsx193

Hi @ntvsx193, could you explain more about what was your approach to send event to WS? I tried using AdonisJS Events too when initializing the socket and but keep getting undefined when I want to broadcast the message, even after the initialization happened.

// start/events.js
Event.on('cardPrice::initialize', (socket) => {
  wsSocket = socket;
  console.log('wsSocket', wsSocket);
});

Event.on('cardPrice::update', async (newCardPriceList) => {
  console.log('wsSocket update', wsSocket); << keep getting undefined even after initialize happened
});


// app/Controller/WS/UpdateCardPriceController.js
class UpdateCardPriceController {
  constructor({socket, request}) {
    this.socket = socket;
    this.request = request;
    Event.fire('cardPrice::initialize', socket);
    console.log('a new subscription for cardPrice topic');
  }
}

AudyOdi avatar Jul 13 '18 02:07 AudyOdi

@AudyOdi I been have this Issues too while I trying to make a database connect in WebSocketController I been thinking in maybe making a service provider and using is I can over this stack

MatheusVSMPimentel avatar Sep 18 '20 11:09 MatheusVSMPimentel