I have an app in Laravel for the backend and Vue.js and Quasar for the front end. I have a view in Vue.js that sends a request every 5 seconds. I would like to know how I can implement web sockets. I already configured Pusher and Laravel Echo. I understand that WebSockets work as a protocol that switches the HTTP protocol and creates an event by which the socket communicates.
At the moment, I have only tried communicating the back socket with the front, but I want the socket to handle or listen to every updated data in my table so that I don’t have to send requests every 5 seconds. What I do in my table is pagination, which is done in the front. The request is sent to the back, and then it shows the data.
Another thing, I have implemented a loading logic so that every time a request is made, the loading is shown, and when the request is processed, the loading is hidden, so the request is made every 5 seconds, and the loading is shown every 5 seconds.
Here’s a snippet of my Vue.js view:
async init() {
this.onRequest({
pagination: this.pagination,
filter: undefined
})
this.timeout = setTimeout(this.init, 5000);
and this is the "onRequest" function
onRequest(props) {
const { page, rowsPerPage } = props.pagination
const { search } = this
this.loading = true
DataService.index({ params: { page, rowsPerPage, search } }).then((payrolls) => {
this.data = payrolls.data
this.pagination.rowsPerPage = payrolls.per_page
this.pagination.page = payrolls.current_page
this.pagination.rowsNumber = payrolls.total
this.loading = false
}).catch(() => {
this.loading = false
})
},
2
Answers
I have all that configured but my question is that, for example, from the back to the front, 100 data are paged. Do I have to send those 100 data to the socket so that the socket is listening to them and verifying in real time if there have been changes? In the back I have my controller I have several methods that page but in the front I only have pages, let's say 10-20, 50 or whatever I choose
This is a part of my controller
this is my event for the socket
on my table view i have this
i'm new at laravel sorry for the inconveniences
As far as I understand, you’re making repetitive AJAX requests within specified time intervals (e.g. 5 seconds) to fetch the data from the server (this is also known as "AJAX polling"), but this methodology creates an HTTP overhead because the client has to repeatedly ask the server for data.
Since you’re on Laravel, you can use Laravel broadcasting features. It requires a quick setup process that I’ll list here briefly just for you to make sure that you’ve setup everything correctly.
The installation process on the back-end part requires the following steps:
Enable the
AppProvidersBroadcastServiceProvider
provider in yourconfig/app.php
by uncommenting it.Install the Pusher Channels PHP SDK:
.env
file:BROADCAST_DRVIER
variable in your project and set it topusher
:QUEUE_CONNECTION
variable to besync
:The installation process on the front-end part requires the following steps:
pusher-js
library and thelaravel-echo
client:resources/js/bootstrap.js
file, make sure to uncomment the following code:Now, to send updates to a client you need to broadcast an event holding this data to the client instead of doing repeated AJAX requests, so you have to do the following:
IlluminateContractsBroadcastingShouldBroadcast
interface, and make sure that thebroadcastOn()
method returns the names of the channels where the event will be broadcasted, in this example we’re broadcasting on a public channel calledmy-channel
:Echo
object which provides you with several methods to interact with the sockets:Notice that within the callback of the
listen()
method you can specify what you want to happen when you receive such an event, and the parameter of the callback holds an object that contains the data broadcasted inside the event.On your back-end, test and broadcast the event using the
broadcast()
method:This is merely just an example, for better documentation and in-depth explanation you should check the documentation at: https://laravel.com/docs/10.x/broadcasting