Search code examples
javascriptlaravelpusherlaravel-livewirelaravel-echo

Get latest event of Pusher channel


I have a live dashboard which displays data from a e-sports match. The data comes in trough pusher on the channel csgo and the event name is match-data-csgo.

Receiving the data when there is a new event is no problem, but I'm looking for a way to get the last event it's value. They pass a json in the event as value and from this json I can fill my page with content.

I'm working with Laravel and Laravel Echo and LiveWire. This is the code that works on a detect of a new event:

window.addEventListener('DOMContentLoaded', function () {
    Echo.channel('csgo')
        .listen('.match-data-csgo', (e) => {

            window.livewire.emit('newCsgoData', e)

        });
})

By emitting (e) to the newCsgoData function in livewire I can process all the data.

I need to have the json value of the last event of pusher so I can preload it on the page, because now the page is blank as long as pushere doesn't receive a new event.


Solution

  • Pusher doesn't store any events or event data so you will need to store the event data on your server and retrieve this on page load. There are a couple common ways to achieve this, for example sending a JSON file on page load that includes the most recent event(s) or exposing an endpoint on your server that sends the latest event to the requester, allowing clients to request during loading to populate the data.