Book Image

Vue.js 3 By Example

By : John Au-Yeung
Book Image

Vue.js 3 By Example

By: John Au-Yeung

Overview of this book

With its huge ecosystem and wide adoption, Vue is one of the leading frameworks thanks to its ease of use when developing applications. However, it can get challenging for aspiring Vue.js developers to make sense of the ecosystem and build meaningful applications. This book will help you understand how you can leverage Vue effectively to develop impressive apps quickly using its latest version – Vue 3.0. The book takes an example-based approach to help you get to grips with the basics of Vue 3 and create a simple application by exploring features such as components and directives. You'll then enhance your app building skills by learning how to test the app with Jest and Vue Test Utils. As you advance, you'll understand how to write non-web apps with Vue 3, create cross-platform desktop apps with the Electron plugin, and build a multi-purpose mobile app with Vue and Ionic. You'll also be able to develop web apps with Vue 3 that interact well with GraphQL APIs. Finally, you'll build a chat app that performs real-time communication using Vue 3 and Laravel. By the end of this Vue.js book, you'll have developed the skills you need to build real-world apps using Vue 3 by working through a range of projects.
Table of Contents (10 chapters)

Adding real-time communication

Now that we are done with adding the routes, authentication, and database configuration and manipulation code, we are ready to add the code for letting us communicate between the frontend and backend in real time. First, we need to create an event class in our Laravel backend so that we can call the event function to broadcast the event as we did in MessageController.

To do that, we run the php artisan make:event MessageSent command to create the MessageSent event class. The class should now be in the backend/app/Events/MessageSent.php file. Once the file is created, we replace what is inside the file with the following code:

namespace App\Events;
class MessageSent implements ShouldBroadcast
    use InteractsWithSockets, SerializesModels;
    public $user;
    public $message;
    public function __construct(User $user, Message