Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Websockets in Tasks #19

Open
vaneetkjoshi opened this issue May 28, 2018 · 4 comments
Open

Use Websockets in Tasks #19

vaneetkjoshi opened this issue May 28, 2018 · 4 comments

Comments

@vaneetkjoshi
Copy link

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

@nrempel
Copy link
Owner

nrempel commented Jun 2, 2018

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

@ntvsx193
Copy link
Collaborator

ntvsx193 commented Jul 8, 2018

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

@AudyOdi
Copy link

AudyOdi commented Jul 13, 2018

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');
  }
}

@MatheusVSMPimentel
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants