Building an online counting application for Laravel and VueJS

Tram Ho

Introduce

In this article we learn how to build an online counting application without having to reload the page. To build the application we use Laravel and Pusher. So what is pusher?

pusher is a cloud service, creating an intermediary server that can help us handle real-time tasks. The data is sent to pusher, and pusher sends it to the registered clients and channels.

Pros and cons of pusher

  • Pros: Short application development time to be able to create a realtime application using pusher without knowing too much knowledge.
  • Disadvantages: Use through an intermediate server so the interaction will be a bit slower but not significant.
  • For applications with high number of users or high number of requests per day, it is necessary to upgrade or use other technologies more optimally.

Install a new application on Pusher

Register an account on Pusher ( link )

Create project laravel and install Pusher SDK, Echo

Create a laravel project

laravel new realtime-counter-laravel-pusher

Next, Next use the composer to install the Pusher PHP SDK

composer requires pusher / pusher-php-server

Next install npm

npm install

Next, install the 2 Laravel Echo and Pusher JS libraries

Laravel Echo is a JavaScript library that it uses to subscribe to channels and listen for events broadcast by Laravel. So, you need to install Echo via NPM package manager. In this example, we also install pusher-js because we need to use Pusher broadcaster:

npm install –save laravel-echo pusher-js

Next run

php artisan make: auth

Project configuration

First create values ​​for APP_ID , APP_KEY , APP_SECRET and APP_CLUSTER in the .env file

Next, at the end of the resources/assets/js/bootstrap.js add the following code:

Online users

In this application, Displays the number of online users on the page when any user who accesses the page or exits the application page updates the online users.

To listen for the event the user accesses or exits the page, it is necessary to use the listen function

  • Join : when the user accesses
  • here : Provide a list of currently accessed users
  • joining : executes when other users join the channel
  • leaving : executes when the user leaves the channel

Authorization

Each channel present is a separate channel, in BroadcastServiceProvider we will set the permissions for the channel. To authenticate allowing the user to listen to a specific channel, in the routes/channels.php add the following code:

As we can see, return is not true or false. If the authenticated user listens on the channel, it will return an array of values ​​including the user’s id and name.

However, we can write a different logic depending on the specific requirements of each application such as:

Vue Component

Above we have installed and configured, followed by displaying the number of people online on the page with vuejs

Below is an illustration of the number of visitors.

Conclude

Above is the article to create an online counting application on the page. The article covered basic concepts as well as configuration settings for building applications using laravel on BE and vue on FE.

References

https://pusher.com/tutorials/counter-laravel

Share the news now

Source : Viblo