What is laravel-echo?
Laravel Echo is a JavaScript library that makes it easy to work with WebSockets in a Laravel application. It provides a simple API for subscribing to channels and listening for events broadcast by your Laravel application.
What are laravel-echo's main functionalities?
Subscribing to Channels
This feature allows you to subscribe to a public channel and listen for events. In this example, the client subscribes to the 'orders' channel and listens for the 'OrderShipped' event.
Echo.channel('orders').listen('OrderShipped', (e) => { console.log(e.order); });
Private Channels
This feature allows you to subscribe to private channels that require authentication. In this example, the client subscribes to a private 'orders' channel specific to a user and listens for the 'OrderShipped' event.
Echo.private('orders.' + userId).listen('OrderShipped', (e) => { console.log(e.order); });
Presence Channels
Presence channels build on the concept of private channels and allow you to track the presence of users in a channel. In this example, the client joins a 'chat' room and logs the users currently in the room, as well as users joining and leaving.
Echo.join('chat.' + roomId).here((users) => { console.log(users); }).joining((user) => { console.log(user.name); }).leaving((user) => { console.log(user.name); });
Notifications
This feature allows you to listen for notifications sent to a user. In this example, the client subscribes to a private channel for a specific user and listens for notifications.
Echo.private('App.User.' + userId).notification((notification) => { console.log(notification.type); });
Other packages similar to laravel-echo
socket.io
Socket.IO is a JavaScript library for real-time web applications. It enables real-time, bidirectional and event-based communication. Compared to Laravel Echo, Socket.IO is more general-purpose and can be used with any backend, not just Laravel.
pusher-js
Pusher is a hosted service that makes it super-easy to add real-time data and functionality to web and mobile applications. The pusher-js library is the client-side component. Compared to Laravel Echo, Pusher provides a hosted solution and can be used with various backend technologies.
ably
Ably is a platform that provides APIs to simplify and overcome the complex aspects of building and maintaining a real-time messaging system. The Ably JavaScript library is the client-side component. Compared to Laravel Echo, Ably offers a more comprehensive set of features and can be used with various backend technologies.
Introduction
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.
Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by Laravel. You may install Echo via the NPM package manager.
Official Documentation
Documentation for Echo can be found on the Laravel website.
Contributing
Thank you for considering contributing to Echo! The contribution guide can be found in the Laravel documentation.
Code of Conduct
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
Laravel Echo is open-sourced software licensed under the MIT license.