adonis-scheduler
adonis-scheduler copied to clipboard
Use Websockets in Tasks
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
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
You can try use AdonisJS Events for send event to WS. I use it in adonis version 3.2
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 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