I there a way to listen event on client side

I have Event listener in my project, I need to show that response in HTML view, How do I display response in view without refreshing page every time (real time).

1 Like

You can use WebSocket for a real time application : https://adonisjs.com/docs/4.1/websocket

1 Like

how do I assign value from Listener class to onMessage() of websocket? I am new to webSockets

I’m not an expert with WebSocket.
You can send data with emit(event, data, [ackCallback]) in your listener

Websockets give you real real-time. But usually it’s easier to implement polling - just ask data from API every x second with fetch() or axios like you would with regular GET request.

And when request comes back with new data, then use document.write() for updating content of page.

Polling has quite a lot of downsides tho. It increases load on server quite a lot and it doesn’t give you actual real time data. It gives you data that updates ever x second. The faster you make it update, the more load will your server have.
Also data might come in random order:
Asking for data in order 1, 2, 3 might return data with 2, 3, 1 (since there is no guarantee requests come back in same order as they were sent)

1 Like