Broadcasting
Masonite comes with a powerful way to broadcast events in your application. These events can be listened to on the frontend using JavaScript/Vue and the Pusher library. These can be things like a new notification which you can show on the frontend without reloading the page.

Broadcasting

You can broadcast a dictionary onto any number of channels:
1
from masonite.broadcasting import Broadcast
2
from app.broadcasts import UserAdded
3
4
def emit(self, broadcast: Broadcast)
5
broadcast.channel('channel_name', "event_name", {"key": "value"})
Copied!
This will emit an event to all applications listening to this public event.
You may broadcast on multiple channels as well:
1
broadcast.channel(['channel1', 'channel2'], "event_name", {"key": "value"})
Copied!
This type of broadcasting will emit all channels as public. For private and presence channels, keep reading.

Creating a Broadcasting Event

Broadcasting events are simple classes that inherit from CanBroadcast. You may use any class, including the Masonite Event classes.
A broadcasting event will look like this:
1
from masonite.broadcasting import CanBroadcast, Channel
2
3
class UserAdded(CanBroadcast):
4
5
# ..
6
7
def broadcast_on(self):
8
return Channel("channel_name")
Copied!

Broadcasting The Event

You can broadcast the event using the Broadcast class:
1
from masonite.broadcasting import Broadcast
2
from app.broadcasts import UserAdded
3
4
def emit(self, broadcast: Broadcast)
5
broadcast.channel(UserAdded())
Copied!

Channel Options

You can broadcast on any numbers of channels.

Public Channels

Inside the event class you can specify a public channel. These channels allow anyone with a connection to listen to events on this channel:
1
from masonite.broadcasting import Channel
2
3
def broadcast_on(self):
4
return Channel("channel_name")
Copied!

Private Channels

Private channels require authorization from the user to connect to the channel. You can use this channel to emit only events that a user should listen in on.
Private channels are channels that start with a private- prefix. When using private channels, the prefix will be prepended for you automatically.
Private channels can only be broadasting on if users are logged in. When the channel is authorized, it will check if the user is currently authenticated before it broadcasts. If the user is not authenticated it will not broadcast anything on this channel.
1
from masonite.broadcasting import PrivateChannel
2
3
def broadcast_on(self):
4
return PrivateChannel("channel_name")
Copied!
This will emit events on the private-channel_name channel.

Routing

On the frontend, when you make a connection to a private channel, a POST requets gets made to authenticate the private channel. Masonite ships with this authentication route for you. All you need to do is add it to your routes:
1
from masonite.broadcasting import Broadcast
2
3
ROUTES = [
4
# Normal route list here
5
]
6
7
ROUTES += Broadcast.routes()
Copied!
This will create a route you can authenticate you private channel on the frontend. The authorization route will be /broadcasting/authorize but you can change this to anything you like:
1
ROUTES += Broadcast.routes(auth_route="/pusher/auth")
Copied!
You will also need to add the /pusher/auth route to the CSRF exemption:
1
class VerifyCsrfToken(Middleware):
2
3
exempt = [
4
'/pusher/auth'
5
]
Copied!

Presence Channels

Presence channels work exactly the same as private channels except you can see who else is inside this channel. This is great for chatroom type applications.
For Presence channels, the user also has to be authenticated.
1
from masonite.broadcasting import PresenceChannel
2
3
def broadcast_on(self):
4
return PresenceChannel("channel_name")
Copied!
This will emit events on the presence-channel_name channel.

Routing

Adding the authentication route is also the exact same as private channels:
1
from masonite.broadcasting import Broadcast
2
3
ROUTES = [
4
# Normal route list here
5
]
6
7
ROUTES += Broadcast.routes()
Copied!
This will create a route you can authenticate you private channel on the frontend. The authorization route will be /broadcasting/authorize but you can change this to anything you like:
1
ROUTES += Broadcast.routes(auth_route="/pusher/auth")
Copied!
Last modified 1mo ago