Two components involved in Broadcasting. Just the communication between Laravel Backend
Everytime you shoot the shoot button confetti's happen in real-time. ( laracon.beyondco.de )
ShootConfetti event. It implements ShouldBroadcast interface. It has broadcastOn() method. It tells Laravel , if event is dispatched and broadcast, where it should be broadcasted to?
There are different channels.
dispatch() method checks if it. should be broadcasted using shouldBroadcast(). If yes, should it broadcasted now or later using broadcastWhen() method.
By default it returns true. to have control over broadcasting the event implement the broadcastWhen method on the event.
broadcast the event and available broadcast drivers like, log, pusher, ably ( never heard about it ), null and redis.
null can be used in CI environment or testing environment.
Redis driver is a low-level driver.
Main essence of Broadcasting is:
Webhooks using Laravel Broadcasting. Wow!
Define a custom broadcasting driver. Do it inside AppServiceProvider
Create a new class called Webhook which implements Broadcaster interface
Few methods like auth, broadcast ( this is what we want )
return our Webhook class from AppServiceProvider
Change broadcast driver to webhook inside .env file
add new webhook connection inside app/broadcasting.php
Put the URL inside Channel() inside broadcastOn() method
We have managed to work with our Backend. Now let our frontend know about event and the data.
We can use Laravel Echo. We can use pusher/ably broadcaster we can just specify that.
Especially useful for real-time communications.
Ajax Polling to fetch messages after few time.
Websocket on the other hand is permanently opened connection which makes it fast. and also help with real-time.
Listening on the public stream and listen for #laracon hashtag and generate HeardTweet and then send it to front-end via Laravel Echo.
Another example. Exporting orders.
User click, he has to wait 10 second.
Second approach, trigger export, it gets queued up, processed in background, you get email that its done.
Another way is to do it in real time.
Exporting
Channels
Two types
Public Channel: exposed to everyone
Private Channel: works same as Public one in the backend.
Echo.private for Private channels instead of Echo.listen
Enable BroadcastServiceProvider because its disabled by default.
Authentication errors on Private channels.
routes/channels.php file and define how we want to authenticate users for different channels
Any authenticated user can join this private channel.
Presence channel works in similar way to Private channel.
Presence channel know about authenticated users who are on the given channel.
Events like .joining() and .leaving() , example of Google Docs
Backend implementation on Laravel. It's pretty simple.
Frontend implementation:
User increment on joining and decrementing on leaving event.
We get auth token from the Backend. For presence channel we also get user information for joining and leaving events.
We can also have b/w one frontend and the other.
Client event in Laravel Echo called, Whispering.
listenForWhisper method from the other frontend clients.
laravel-websockets v2 is coming soon which will bring horizontal scaling.