I am making some Real Time data application and I want display total number of user registered to my site.
The count should refresh automatically whenever a new user registers without refreshing page.
How can I achieve this?
I am using php, Laravel and MySQL
Use brodacasting feature.
In many modern web applications, WebSockets are used to implement realtime, live-updating user interfaces. When some data is updated on the server, a message is typically sent over a WebSocket connection to be handled by the client. This provides a more robust, efficient alternative to continually polling your application for changes.
To assist you in building these types of applications, Laravel makes it easy to "broadcast" your events over a WebSocket connection. Broadcasting your Laravel events allows you to share the same event names between your server-side code and your client-side JavaScript application.
EDITED:
What you need is called Broadcasting
. You need to implement 2 things
The basic idea is when you register an user, if succeed, at this time call the Publisher to send a message to it's subscribers to update the user count.(just like when you subscribe to someone youtube channel, when they upload a new video, you get a notification)
you can use https://socket.io/ to implement your client side
Laravel supported it also, find the Socket.io section laravel
WebSocket absolutely the correct answer for this question. But for beginner I think you can always use setInterval
and good old ajax
.
I am not saying this is the most correct answer, but this would be much more simpler and easier.
You can use Laravel's Broadcasting, Laravel supports redis and pusher by default.,
Configuring Broadcast
There are set of configuration in config/broadcasting.php
, just add the required configurations in the file directly or through .env
Before broadcasting any events, you will first need to register the app/Providers/BroadcastServiceProvider
. In fresh Laravel applications, you only need to uncomment
this provider in the providers array of your config/app.php
configuration file.
I personally using Pusher service, you will get a PUSHER_APP_ID
, PUSHER_APP_KEY
and PUSHER_APP_SECRET
.
Configuring Laravel -Echo (Js - to receive the broadcast)
while using pusher, use laravel-echo js to listen for events broadcast by Laravel, you can install through npm install
npm install --save laravel-echo pusher-js
In your app.js just add this to import and initialize the Echo and store in a window object
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your-pusher-key'
});
Sending Broadcast
when defining broadcast events there are three different methods available broadcastOn
, broadcastWith
, broadcastData
. Broadcast by triggering the event
event(new youBroadcastClassName($array))
Receiving Broadcast
Echo.channel('channel-name')
.listen('youBroadcastClassName', (e) => {
console.log(e);
});
now it is available in the js you can use some js functions to show in the front end.
In your case once user is registered trigger the broadcast event with data which you need in front end, just receive in the js and populate it in the view
Go through this link Laravel Broadcasting