You get a bonus - 1 coin for daily activity. Now you have 1 coin

How WebSocket works in Laravel: real-time applications using Echo / Reverb

Lecture



Modern messaging systems require not only high delivery speed but also guaranteed addressing: each message must reach exactly the user it is intended for. In web chats this is especially critical — the correct routing determines the convenience of communication, the security, and the scalability of the whole platform. Laravel provides convenient tools for organizing WebSocket connections and building message-switching logic. In this article we'll look at the basic principles of addressed delivery, architectural approaches, and practical solutions that let you implement a reliable chat system on Laravel.

In Laravel the principle is this: WebSocket does not “look for the user itself”, but delivers an event to a channel, and it's the channel that makes the message available only to the clients who are allowed to subscribe to it. For chat between specific users, private channels are usually used, and for group rooms with information about who's present — presence channels. Laravel uses broadcasting for this, and as a first-party WebSocket server it now offers Laravel Reverb; also, an event can be sent with toOthers() so it isn't returned to the same client who just sent the message.

The main idea of using WebSocket in Laravel

A chat system has 5 roles:

  1. Sender — user A, through a regular fetch/ajax/axios call, and subscribes to channels via Pusher Js + Laravel Echo
  2. Laravel backend — accepts the HTTP request “send message”
  3. DB — stores the message (not mandatory)
  4. Broadcasting / WebSocket server — distributes the event to the right channel
  5. Receiver — user B, who is subscribed to that channel using Pusher Js (WebSocket communication and handling) + Laravel Echo (a convenient interface for developers)

That is, the logic does not work like:
“send directly to user B's socket”

But like:
“send an event to channel chat.15, and only the participants of that conversation have access to it”

How WebSocket works in Laravel: real-time applications using Echo  Reverb

What this looks like step by step

Let's say there is a private chat between user 5 and user 9.

Step 1. Both clients open a WebSocket connection

On the frontend, both users connect via Echo / Reverb.

Step 2. Each subscribes to the private channel of the conversation

For example:

private-chat.42

Where 42 is the id of the conversation, not the id of the user.

Step 3. Laravel checks whether the current user has the right to enter the channel

This is done via Broadcast::channel(...) in routes/channels.php.
Laravel requires authentication callbacks for private/presence channels, and access to such channels is authorized separately.

Step 4. User A sends a message via a regular HTTP request

Usually:

  • POST /messages
  • the backend validates it
  • saves the message to the DB

Step 5. After saving, Laravel fires a broadcast event

For example MessageSent.

Step 6. The event is published to the chat channel

All clients subscribed to this channel who have passed authorization receive the event via WebSocket.
No one outside the channel receives this message.

Why messages go “between specific users”

Because the restriction is applied not at the level of the WebSocket connection, but at the level of:

  • which channel the event concerns

  • who has the right to subscribe to that channel

This is the most important principle.

That is, message “switching” in a chat is essentially:

message -> event -> channel -> authorized subscribers

What is best to use as a channel

For private messages there are usually 2 sound approaches.

Option 1. Conversation channel

For example:

chat.42

Where 42 is a private conversation between two people.

This is the best option because:

  • it's easy to store history
  • it's easy to check participation in the conversation
  • it's convenient to scale
  • the same channel works for both sides

Access check

The idea is this:

Broadcast::channel('chat.{conversationId}', function ($user, $conversationId) {
return \App\Models\Conversation::query()
->whereKey($conversationId)
->whereHas('participants', fn ($q) => $q->where('users.id', $user->id))
->exists();
});

The point: only someone who is actually a participant of the conversation will enter the channel.

Option 2. Personal user channel

For example:

users.9

Then the server sends the event specifically to the recipient's channel.

This is suitable for:

  • personal notifications
  • unread message counters
  • “you have a new message”
  • system events

But the actual chat text itself is more convenient to run through the conversation channel rather than through the user channel.

Correct architecture for a private chat

a sequence diagram for a chat built on Laravel + WebSocket.

Below is an example for the scenario:

  • user A opens the chat
  • subscribes to the private channel
  • sends a message to user B
  • Laravel saves the message
  • the event goes out over WebSocket
  • user B receives the message instantly

Sequence diagram of a chat on Laravel + WebSocket.

How WebSocket works in Laravel: real-time applications using Echo  Reverb

What happens here step by step

1. WebSocket connection

Both clients open a persistent connection to the WebSocket server.

2. Channel authorization

For a private channel Laravel checks whether the user has the right to listen to this chat.

Usually this is routes/channels.php:

Broadcast::channel('chat.{conversationId}', function ($user, $conversationId) {
return ConversationParticipant::query()
->where('conversation_id', $conversationId)
->where('user_id', $user->id)
->exists();
});

3. Subscribing to the channel

If auth succeeds, the client subscribes to:

private-chat.42

4. Sending the message

User A sends a regular HTTP POST to Laravel.

5. Saving to the DB

Laravel saves the message to the messages table.

6. Broadcasting the event

Laravel calls an event like MessageSent, and it goes to the WebSocket server.

7. Delivery to subscribers

The WebSocket server distributes the event to everyone subscribed to private-chat.42.

Simplified diagram

User A
│
│ POST /messages
▼
Laravel
│
│ save
▼
Database
│
│ broadcast MessageSent
▼
WebSocket Server
│
├──> User B
└──> User A (optional)

Important note

Usually the message travels like this:

  • to the DB — to store history
  • to WebSocket — to instantly show the message to online users

That is, WebSocket doesn't replace the DB, it complements it.

The toOthers() variant

If you don't want the sender to receive their own message back over the socket:

broadcast(new MessageSent($message))->toOthers();

Then the sequence will be like this:

How WebSocket works in Laravel: real-time applications using Echo  Reverb

If user B is offline

Then the WebSocket part won't work for them at that moment, but the message will still:

  • be saved to the DB
  • be available the next time the chat is opened via the API

That is:

if online -> receives via WebSocket
if offline -> reads later from the DB

Minimal system participants

A Laravel chat usually involves:

  • Frontend + Laravel Echo
  • Laravel Controller
  • Message/Event
  • Broadcast channel auth
  • WebSocket server (Reverb / Pusher)
  • Database

A more general “technical” diagram

How WebSocket works in Laravel: real-time applications using Echo  Reverb

Usually it's done like this:

1. Conversation channel

For new messages in a conversation:

  • private-chat.{conversationId}

2. User channel

For personal things:

  • private-user.{userId}

For example, you'd send there:

  • unread badge updates
  • “the other person is typing”
  • “message delivered”
  • “message read”

That way the architecture ends up cleaner.

How Laravel wires this together in practice

1. The event

use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessageSent implements ShouldBroadcast
{
public function __construct(public Message $message)
{
}

public function broadcastOn(): array
{
return [
new PrivateChannel('chat.' . $this->message->conversation_id),
];
}

public function broadcastAs(): string
{
return 'message.sent';
}

public function broadcastWith(): array
{
return [
'id' => $this->message->id,
'conversation_id' => $this->message->conversation_id,
'sender_id' => $this->message->sender_id,
'text' => $this->message->text,
'created_at' => $this->message->created_at?->toIso8601String(),
];
}
}

The key points here:

  • ShouldBroadcast — Laravel understands that the event needs to be sent out
  • PrivateChannel — the channel is private
  • broadcastWith() — exactly what the frontend will receive

2. Sending after saving

public function store(StoreMessageRequest $request)
{
$conversation = Conversation::findOrFail($request->conversation_id);

abort_unless(
$conversation->participants()->where('users.id', auth()->id())->exists(),
403
);

$message = Message::create([
'conversation_id' => $conversation->id,
'sender_id' => auth()->id(),
'text' => $request->text,
]);

broadcast(new MessageSent($message))->toOthers();

return response()->json([
'status' => 'ok',
'message' => $message,
]);
}

toOthers() is useful so that the current client doesn't receive its own broadcast a second time, if it has already optimistically shown the message locally. Laravel directly supports this scenario for broadcast events.

How the frontend subscribes

Roughly like this:

Echo.private(`chat.${conversationId}`)
.listen('.message.sent', (e) => {
console.log('New message', e);
addMessageToChat(e);
});

The point:

  • the client subscribes to the private channel
  • Laravel checks the access right
  • after that the client only receives events for this chat

Where exactly the “routing” happens

Here's the main answer to your question about the switching principle:

The WebSocket server doesn't decide the business logic

It usually only knows:

  • there's a connection
  • the connection is subscribed to a channel
  • the event was sent to the channel

Laravel decides:

  • who the current user is
  • whether they're allowed into private-chat.42
  • which channel to publish the event to
  • which data may be given out

That is, the business switching is located in Laravel:

  • the conversation model
  • channel authorization
  • channel selection when broadcasting

How to check access correctly

In routes/channels.php:

use Illuminate\Support\Facades\Broadcast;
use App\Models\Conversation;

Broadcast::channel('chat.{conversationId}', function ($user, $conversationId) {
return Conversation::query()
->whereKey($conversationId)
->whereHas('participants', function ($q) use ($user) {
$q->where('users.id', $user->id);
})
->exists();
});

This is the foundation of security.
If you do it poorly, someone else's user will be able to subscribe to someone else's chat.

How the tables are usually set up

At a minimum:

users

  • id
  • name

conversations

  • id
  • type (private, group)

conversation_user

  • conversation_id
  • user_id

messages

  • id
  • conversation_id
  • sender_id
  • text
  • created_at

Then the access check is very natural:
is the user present in conversation_user.

What happens on a private message from user A -> user B

Example:

  • A writes to B
  • the server finds or creates a private conversation
  • saves the message in messages
  • sends an event to chat.{conversationId}
  • both A and B are subscribed to this channel
  • B receives the message instantly
  • A doesn't need to be sent it again, thanks to toOthers()

And what if the user is offline?

Then WebSocket won't deliver anything right away.
That's why a chat is almost always built like this:

  1. the source of truth is stored in the DB
  2. WebSocket — only real-time delivery
  3. if the user was offline, they simply read the history from the API / DB afterward

This is a very important principle.
WebSocket in a chat is not storage, it's a fast transport.

What else is usually sent over channels

Besides the message text itself:

To the chat channel

  • new message
  • message edit
  • message deletion
  • typing
  • read receipt

To the personal user channel

  • total unread count
  • new-conversation notification
  • system notifications

Private channel or presence channel?

Private channel

Presence channel

Needed when you just need to restrict access.

Suitable for:

  • private conversations
  • closed groups
  • private notifications

This is a private channel + a list of the channel's participants.
The Laravel docs separately describe that presence channels support the same broadcast events,

but also let you know who is currently in the room.

Suitable for:

  • “who's online in the room right now”
  • “user is typing”
  • the list of those present in a group chat

For a simple private chat a private channel is usually enough.

The most common mistake when working with channels in Laravel

Beginners often make the channel like this:

chat.{userId}

And try to send “to the user.”
This works for notifications, but isn't very convenient for a full-fledged conversation.

It's better to think not “to whom”, but “which conversation does the event belong to.”

That is, not:

  • “send to user 9's socket”

But:

  • “publish an event to conversation 42”

Practical diagram for working with WebSockets in Laravel

How WebSocket works in Laravel: real-time applications using Echo  Reverb

If toOthers() is used, the event won't come back to the same socket A, while B will get it right away.

Recommendations for working with WebSockets in Laravel

For a modern implementation:

  • use Laravel broadcasting
  • WebSocket server — Laravel Reverb
  • for conversations — a private channel by conversationId
  • for user notifications — a private channel by userId
  • keep channel authorization in routes/channels.php

Laravel now officially promotes Reverb as the first-party solution for real-time WebSocket communication, and setting up broadcasting is supported via an artisan command.

Laravel Echo itself never makes requests to the DB.
On the frontend, Echo only:

  • opens a WebSocket connection,
  • subscribes to a channel,
  • listens for events,
  • sometimes makes an HTTP request to authorize a private / presence channel.
    Working with the DB is already your Laravel code: controller, service, model, repository, and so on.

How this actually happens

For a private chat the chain is usually as follows:

  1. the frontend sends a regular POST /messages
  2. Laravel saves the message to the DB
  3. Laravel fires a broadcast event
  4. the broadcaster / Reverb publishes this event to the right channel
  5. all already-connected and authorized subscribers of this channel receive the payload via WebSocket.

That is, the DB is usually involved at the moment the message is created, because the message must be saved as the source of truth. But the actual delivery of the payload to subscribers happens not through the DB, but through broadcasting + the WebSocket server.

Is the message body distributed without repeated DB queries?

Yes, that's usually exactly how it's done.
If you've already created the Message and formed broadcastWith(), or simply passed the needed fields into the event, then the WebSocket server distributes this payload to subscribers without “endless DB requests.” Recipients listen for the event over the open socket, not by polling via SQL.

Roughly the logic is this:

$message = Message::create([
'conversation_id' => $conversation->id,
'sender_id' => auth()->id(),
'text' => $request->text,
]);

broadcast(new MessageSent(
id: $message->id,
conversationId: $message->conversation_id,
senderId: $message->sender_id,
text: $message->text,
))->toOthers();

And further in the event:

public function broadcastWith(): array
{
return [
'id' => $this->id,
'conversation_id' => $this->conversationId,
'sender_id' => $this->senderId,
'text' => $this->text,
];
}

Then the subscribers get already-ready data.

But “in an infinite loop on Node.js” — that's not quite accurate

If you have a modern Laravel stack with Reverb, then it's not a Node.js loop, but a separate Reverb WebSocket server, which holds persistent connections and distributes broadcast events across channels. Laravel directly describes Reverb as a first-party WebSocket solution integrated with broadcasting.

If, on the other hand, an old stack via third-party services or an old socket.io/node server is used, then yes — there could be Node.js there. But Laravel Echo by itself doesn't mean Node.js. Echo is a client-side library, and the server side can be Reverb or another broadcaster.

Where there can still be additional calls not to the DB but to the server

There are two important places:

1. Private channel authorization

When the client subscribes to private-chat.42, Laravel performs an auth request and the callback in Broadcast::channel(...), to check whether the user has access to the channel. This is an HTTP auth step, and inside it you yourself can make a DB request, for example to check the user's participation in the conversation.

2. Queues

Broadcast events in Laravel are often sent through a queue rather than synchronously within the same request. Then, after the message is saved, the event goes into a queue, and a worker publishes it to the WebSocket broadcaster. This doesn't mean constant DB requests for every subscriber, but a queue can be involved on the infrastructure side.

The correct mental model

Not like this:

Echo -> DB -> Node.js -> DB -> subscriber -> DB ...

But like this:

frontend -> Laravel HTTP -> save in DB -> broadcast event -> WebSocket server -> subscribed clients

That is:

  • the DB is needed to store history and business logic,
  • Echo is needed for subscribing and receiving events,
  • Reverb / the broadcaster is needed for real-time distribution,
  • you don't need a constant loop of SQL queries for every listener.

When you call toOthers(), Laravel uses the socket ID that Echo passes via X-Socket-ID, so as not to send the broadcast back into that same connection. This is also part of the delivery mechanics, and it doesn't require the DB for filtering out the current socket itself.

Bottom line

Yes, in a proper implementation:

  • the message is saved to the DB once,
  • after that its payload is distributed to subscribers via WebSocket,
  • Echo doesn't go to the DB,
  • there's no endless loop of DB queries for distribution,
  • but when subscribing to a private channel there's usually an auth check, which may hit the DB,
  • and the distribution server in modern Laravel is more often Reverb, not necessarily Node.js.

I can further draw you an exact diagram of the private-message flow in Laravel: HTTP → DB → Event → Queue/Reverb → Echo, explaining exactly where DB requests happen and where they no longer do.

How does the infinite loop (event loop) in Node Js differ from the Reverb WebSocket server

the difference “in whether there's an infinite loop” is almost none — it's there everywhere.
The difference is in who controls this loop and at what level you work.

The essence of the difference

Node.js (a hand-rolled server)

You write the logic yourself:

  • opening connections
  • storing clients
  • routing messages
  • authorization
  • scaling

That is, you control the event loop directly through code

Laravel Reverb

This is already a ready-made WebSocket server on top of the PHP/Laravel ecosystem:

  • the event loop is hidden inside
  • you don't work with sockets directly
  • you work through broadcast events + channels

That is, the loop exists, but you don't see it or control it directly

What the “infinite loop” actually is

In Node.js

In Reverb

This is a real event loop:

while (true) {
processEvents();
}

(simplified)

There's an event loop there too (usually based on ReactPHP / a Swoole-like model),
but you don't touch it.

It does:

while (true):
listen to sockets
accept events
distribute across channels

It:

  • listens to sockets
  • accepts events
  • calls callbacks

You embed yourself right into it:

ws.on('message', (msg) => {
// your logic
});

BUT you interact like this:

broadcast(new MessageSent($message));

And that's it.

The main difference

Node.js — a low-level approach

You're thinking about:

  • “who to send to”
  • “where to store connections”
  • “how to find the right user”
  • “how to synchronize multiple servers”

Example:

clients[userId].send(message);

You implement the switching yourself

Reverb — a high-level approach

You're thinking about:

  • “which channel to send to”
  • “who has access to the channel”
broadcast(new MessageSent())->to(new PrivateChannel('chat.42'));

Laravel does the switching through channels

Architectural difference

Node.js Reverb

 

How WebSocket works in Laravel: real-time applications using Echo  Reverb

How WebSocket works in Laravel: real-time applications using Echo  Reverb

You yourself:

  • store a map of users
  • decide who to send to

Reverb:

  • doesn't know the business logic
  • just knows: “this socket is subscribed to channel X”

Key difference in thinking

“Send a message to user B”

Key difference in thinking

Publish an event to channel chat.42”

Where access is checked

You write it yourself:

if (user.id !== message.targetUserId) return;

Where access is checked

Broadcast::channel('chat.{id}', function ($user, $id) {
return in_array($user->id, conversationParticipants($id));
});

The check happens once, at subscription time, not on every send

Performance

  • very fast
  • flexible
    − you have to optimize everything yourself
    − easy to mess up with memory / leaks

Performance

  • faster to develop
  • built into Laravel
  • safer out of the box
    − less flexibility
    − you depend on Laravel's architecture
  • you write the WebSocket server yourself
  • you manage the connections yourself
  • you route the messages yourself
  • full control
  • ready-made WebSocket server
  • you work through Laravel events
  • routing through channels
  • the loop is hidden inside
Node.js — you write the transport yourself Reverb — you use Laravel's ready-made transport system

Important point

an infinite loop is the architecture of any WebSocket server:

  • Node.js → event loop
  • Reverb → event loop
  • Go → goroutines
  • Java → threads / NIO

The difference isn't in the loop, but in the level of abstraction.

Here's a detailed comparison of Laravel Reverb vs Socket.IO vs Pusher across all key criteria

Comparison table

Criterion Laravel Reverb Socket.IO Pusher
Solution type Self-hosted (Laravel-first) Self-hosted (Node.js) SaaS (cloud)
Abstraction level High-level (via Laravel events/channels) Medium (API + manual logic) Very high-level
Server language PHP (Laravel) JavaScript / Node.js Not needed
Need to write a WebSocket server? no yes no
Control over logic Medium Maximum Minimum
Message switching Via channels (Laravel) Manually (rooms / sockets) Via channels
Channel authorization Built in (Laravel auth) Needs to be implemented Built in
Working with users Via the Laravel user model You implement it yourself Via API
Integration with Laravel native via API via SDK
Frontend library Laravel Echo Socket.IO client Pusher JS / Echo
Needs a separate server yes (Reverb) yes (Node.js) no
Startup complexity low high very low
Flexibility Medium very high Low
Scaling Via Laravel infra (Redis, queues) Yourself (Redis adapter, etc.) Automatic
Horizontal scaling Needs configuring Needs configuring Out of the box
Presence channel support yes needs to be implemented yes
Private channel support yes manually yes
Fallback (long polling) no yes yes
Works without WebSocket (firewall) no yes yes
Reliability Depends on you Depends on you high
Latency Low Low Medium (via cloud)
Message storage no (your DB) no no
Queues built in (Laravel) yourself not needed
Broadcast events native yourself available
DevOps complexity Medium high minimal
Cost Free Free paid
Vendor lock-in no no yes
Suitable for chats ideal ideal ideal
Suitable for high-load yes, but needs tuning yes yes
Suitable for MVP yes hard ideal
Suitable for enterprise yes yes yes
Key differences

the best choice if you're on Laravel

  • you don't think about sockets
  • you write broadcast(new Event)
  • channels + authorization are already there
  • minimal barrier to entry

downside: less flexible than Node.js

if you need full control

  • you manage the connections yourself
  • you write the message routing yourself
  • you can do whatever you want

downside:

  • a lot of code
  • you need to think about scaling

if you don't want a server at all

  • just an API
  • you don't think about WebSocket
  • everything works “out of the box”

downside:

  • paid
  • dependency on the service
When to choose what

Choose Reverb if:

  • you're on Laravel
  • you need chat / notifications
  • you don't want Node.js
  • you want it fast and done right

Choose Socket.IO if:

  • you need custom logic (games,
  • streaming, complex realtime stuff)
  • you want full control
  • you're ready to write infrastructure

Choose Pusher if:

  • you need a fast MVP
  • you don't have time for DevOps
  • cost isn't critical
Model event → channel → subscribers socket → user → emit API → channel → subscribers

Conclusions

Switching messages between specific users in WebSocket chats is a fundamental element of any modern communication platform. Using Laravel's capabilities, a developer gets a flexible and scalable tool for building private channels, managing subscriptions, and routing events. A correct architecture not only simplifies maintaining and extending the system, but also provides users with stable and secure messaging. Once you've mastered these principles, you can build chats that are equally effective for small teams and large communities alike.

Reverb — the Laravel way (a balance of simplicity and control)
Socket.IO — low-level power and flexibility
Pusher — zero-backend quick start

Message switching in a Laravel chat is done through broadcast channels: the server saves the message, publishes an event to the private channel of the conversation, and only the users whom the authorization callback allowed into that channel can receive it.

Self-check tests

1. Which Laravel component is responsible for sending events to the WebSocket server?

  • A) Queue Worker
  • B) Broadcasting *
  • C) Middleware
  • D) Scheduler

Hint: this is the mechanism that "broadcasts" events outward.

2. Which package is used on the frontend to subscribe to Laravel channels?

  • A) Axios
  • B) Vue Router
  • C) Laravel Echo *
  • D) Blade

Hint: its name matches the effect of a repeating sound.

3. What does WebSocket do differently from regular HTTP?

  • A) Only sends JSON
  • B) Allows two-way communication *
  • C) Only works via POST
  • D) Requires a page reload

Hint: the connection doesn't close after the response.

4. Which server is used in Laravel for WebSocket out of the box (the new approach)?

  • A) Apache
  • B) Nginx
  • C) Reverb *
  • D) Redis

Hint: the name is related to the "reverberation" effect.

5. What is a channel in the context of Laravel Echo?

  • A) A table in the DB
  • B) A task queue
  • C) A log file
  • D) A logical group of subscribers *

Hint: several users can "listen" to the same thing.

6. Which channel type requires user authorization?

  • A) Public channel
  • B) Private channel *
  • C) Static channel
  • D) Global channel

Hint: not every user can get in there.

7. Where are the access rules for channels usually described?

  • A) routes/web.php
  • B) config/app.php
  • C) database.php
  • D) routes/channels.php *

Hint: the filename literally says "channels".

8. What does the broadcast() method do in Laravel?

  • A) Saves data
  • B) Sends an HTTP request
  • C) Distributes the event to subscribers *
  • D) Deletes a record

Hint: it's like "broadcasting."

9. Which property of an event indicates that it should be broadcast?

  • A) implements ShouldQueue
  • B) implements ShouldBroadcast *
  • C) implements Serializable
  • D) implements Dispatchable

Hint: the interface name speaks for itself.

10. How is the event passed from the backend to the WebSocket server?

  • A) Via HTML
  • B) Via FTP
  • C) Via a broadcasting driver *
  • D) Via cookies

Hint: there are drivers like redis, pusher.

11. Which driver is often used to transmit events?

  • A) SMTP
  • B) Redis *
  • C) FTP
  • D) XML

Hint: fast in-memory storage.

12. What does Laravel Echo do on the client?

  • A) Generates HTML
  • B) Subscribes and listens to events *
  • C) Runs migrations
  • D) Handles forms

Hint: it "listens" to the server.

13. What happens if the WebSocket connection drops?

  • A) The server crashes
  • B) The data gets deleted
  • C) The client tries to reconnect *
  • D) All users get logged out

Hint: libraries usually restore the connection automatically.

14. What's the advantage of WebSocket over polling?

  • A) Less code
  • B) No server needed
  • C) Instant delivery without extra requests *
  • D) Only GET requests

Hint: the server itself pushes the data.

15. Which channel type allows one-to-one communication?

  • A) Public
  • B) Presence
  • C) Private *
  • D) Static

Hint: access control is needed.

16. What does a Presence channel add?

  • A) Caching
  • B) A list of participants *
  • C) Logs
  • D) A queue

Hint: you can find out who's online.

17. Where is the event handled after the client receives it?

  • A) In Blade
  • B) In CSS
  • C) In a JavaScript callback *
  • D) In SQL

Hint: frontend logic.

18. What is Reverb in Laravel?

  • A) An ORM
  • B) A WebSocket server *
  • C) An HTTP client
  • D) A CLI tool

Hint: it holds persistent connections.

19. Which event most often triggers sending a message in a chat?

  • A) Migration
  • B) HTTP request *
  • C) CSS update
  • D) Cron

Hint: the user clicks "send."

20. Is it mandatory to save the message to the DB before sending it?

  • A) Yes always
  • B) No, it can be sent directly *
  • C) Only via Redis
  • D) Only via API

Hint: the DB is optional in real-time systems.

21. Why is a queue often used in real-time applications when broadcasting?

  • A) To reduce the JSON size
  • B) To delay sending email
  • C) So as not to block the main HTTP request *
  • D) To replace WebSocket

Hint: the user shouldn't have to wait for heavy operations to finish.

22. Which architectural pattern is implemented when using events and subscribers?

  • A) MVC
  • B) Singleton
  • C) Observer *
  • D) Factory

Hint: there are "observers" that react to changes.

23. Why might Redis be used between Laravel and the WebSocket server in a real-time system?

  • A) To store HTML
  • B) To relay messages as a broker *
  • C) To render pages
  • D) To route HTTP

Hint: a fast intermediary between components.

24. Why is the WebSocket server usually separated from the Laravel backend?

  • A) Because Laravel doesn't support PHP
  • B) For scaling and handling persistent connections *
  • C) To reduce CSS
  • D) For working with Blade

Hint: thousands of connections require separate optimization.

25. What happens after dispatching an event with ShouldBroadcast?

  • A) It's immediately saved to a file
  • B) It enters the broadcasting pipeline *
  • C) It gets deleted
  • D) An SQL join is executed

Hint: the event passes through the broadcasting driver.

26. What risk arises from using only public channels?

  • A) Loss of CSS
  • B) Increased latency
  • C) Data leak *
  • D) JSON error

Hint: everyone has access without a check.

27. How does a Presence channel know the list of users?

  • A) Via an HTML form
  • B) Via cookies
  • C) Via the join/leave event mechanism *
  • D) Via cron

Hint: users "join" and "leave" the channel.

28. Why is WebSocket better suited for chats than polling?

  • A) Because the server is cheaper
  • B) No server is needed
  • C) Instant push messages *
  • D) Only works with GET

Hint: there's no need for constant requests from the client.

29. Which component is responsible for authorizing a private channel?

  • A) Blade
  • B) HTTP Middleware
  • C) The callback in channels.php *
  • D) CSS

Hint: the check is described in a special channels route file.

30. What happens if Redis is unavailable during broadcasting?

  • A) Everything will work faster
  • B) Messages won't be delivered *
  • C) HTML will break
  • D) CSS will update

Hint: the event-delivery chain is broken.

31. How can you reduce the load with a large number of events?

  • A) Increase CSS
  • B) Use batching or throttling *
  • C) Remove WebSocket
  • D) Only use GET

Hint: not every event needs to be sent instantly one by one.

32. Why is it important to serialize data in an event?

  • A) To reduce HTML
  • B) To transmit the data over the network *
  • C) To preserve CSS
  • D) To speed up Blade

Hint: the data must be suitable for transmission.

33. Which approach is better for scaling a WebSocket server?

  • A) One server for everything
  • B) Horizontal scaling *
  • C) Removing Redis
  • D) Using FTP

Hint: several instances work in parallel.

34. What does the "pusher" driver do in Laravel?

  • A) Generates HTML
  • B) Implements broadcasting through an external service *
  • C) Runs cron
  • D) Works with CSS

Hint: it's a SaaS for WebSocket.

35. Why is it important to minimize the payload of events?

  • A) To increase CSS
  • B) To reduce latency and load *
  • C) To preserve HTML
  • D) To speed up Blade

Hint: the smaller the data, the faster the delivery.

36. What happens when there is a large number of open WebSocket connections?

  • A) Nothing
  • B) The load on the server increases *
  • C) CSS disappears
  • D) HTML updates

Hint: every connection requires resources.

37. Which layer is responsible for updating the UI after receiving an event?

  • A) MySQL
  • B) PHP backend
  • C) JavaScript frontend *
  • D) Redis

Hint: the changes happen in the browser.

38. What happens if the client isn't subscribed to a channel?

  • A) It gets all the messages
  • B) It only gets errors
  • C) It doesn't get the events *
  • D) The server crashes

Hint: subscription is mandatory.

39. Which mechanism helps avoid duplicate messages?

  • A) CSS
  • B) Idempotency or unique IDs *
  • C) HTML
  • D) FTP

Hint: every message must be unique.

40. What is the basic principle of real-time architecture?

  • A) The client keeps asking
  • B) The server pushes changes *
  • C) Everything is stored in HTML
  • D) Only POST requests

Hint: the initiator is the server, not the client.

41. Which limitation is one of the key ones for WebSocket connections?

  • A) Can't send JSON
  • B) A limited number of simultaneous connections *
  • C) Only works via POST
  • D) No browser support

Hint: every client keeps an open connection.

42. Why can WebSocket be a problem when working through a proxy or firewall?

  • A) Because it uses CSS
  • B) Because it doesn't support HTTP
  • C) It can be blocked or unsupported *
  • D) It requires FTP

Hint: not every network likes persistent connections.

43. What happens with a poor internet connection when using WebSocket?

  • A) All data is saved automatically
  • B) The connection may drop frequently *
  • C) The server shuts down
  • D) JSON breaks

Hint: the connection has to be stable.

44. Why is WebSocket harder to scale than HTTP?

  • A) Because it uses XML
  • B) Because of state (stateful connections) *
  • C) Because of CSS
  • D) Because it's slow

Hint: the connection needs to be "held" on the server.

45. Which limitation is related to server memory?

  • A) Storing HTML
  • B) Each connection consumes resources *
  • C) JSON is too large
  • D) There are no limits

Hint: thousands of users = thousands of connections.

46. Why can't HTTP be completely replaced by WebSocket?

  • A) WebSocket doesn't support data
  • B) It's not suitable for all types of requests *
  • C) It's always faster
  • D) It doesn't work with JSON

Hint: CRUD requests are more convenient over HTTP.

47. What limitation do mobile clients have?

  • A) No JS support
  • B) The connection can be put to sleep or closed by the system *
  • C) No internet
  • D) No JSON

Hint: the OS saves battery.

48. Why is it important to limit the payload size in WebSocket?

  • A) Because you can't send text
  • B) Large messages increase latency *
  • C) JSON is forbidden
  • D) The server doesn't accept data

Hint: the more data there is, the slower the transfer.

49. Which limitation is related to load balancers?

  • A) They don't support HTTP
  • B) Sticky sessions or shared state are required *
  • C) They only work with CSS
  • D) They remove JSON

Hint: the client needs to land on the same server.

50. Why is a fallback (e.g. polling) sometimes necessary?

  • A) To speed up CSS
  • B) In case WebSocket is unavailable *
  • C) For HTML to work
  • D) For the JSON format

Hint: not every client supports WebSocket. A fallback in WebSocket is a mechanism used when a direct WebSocket connection is impossible (for example, due to proxy or firewall restrictions). In such cases the application automatically switches to alternative protocols (usually long HTTP requests, polling, or streaming), while keeping the same API for the developer.

51. What is the key difference in data transmission between WebSocket and WebRTC?

  • A) WebSocket only works with video
  • B) WebRTC always requires HTTP
  • C) WebSocket goes through a server, while WebRTC can go directly between clients *
  • D) WebRTC doesn't transmit data

Hint: one option uses a centralized server, the other — peer-to-peer.

52. Which connection type is used in WebRTC for data transfer between clients?

  • A) Only HTTP
  • B) Only FTP
  • C) Peer-to-peer connection *
  • D) Only through a database

Hint: clients can communicate directly without a permanent intermediary server.

53. Why is WebRTC more complex to set up than WebSocket?

  • A) Because it doesn't support JavaScript
  • B) It requires STUN/TURN servers and a signaling process *
  • C) It doesn't work in the browser
  • D) It only uses JSON

Hint: additional servers are needed to establish the connection.

54. What's the difference in data packet composition between WebSocket and WebRTC?

  • A) WebSocket always transmits video
  • B) WebRTC packets can include media (audio/video) and metadata, WebSocket — only arbitrary data *
  • C) WebSocket has no headers
  • D) WebRTC doesn't transmit binary data

Hint: one protocol is oriented toward media streams.

55. Which protocol underlies packet transmission in WebRTC?

  • A) TCP IP
  • B) HTTP JSON
  • C) UDP (via SRTP/DTLS) *
  • D) FTP NOP

Hint: a protocol optimized for minimal latency is used.

56. How does the packet structure of WebSocket differ from WebRTC DataChannel?

  • A) WebSocket uses frames over TCP, while WebRTC DataChannel uses messages over SCTP/UDP *
  • B) WebRTC has no packet structure
  • C) WebSocket doesn't support binary data
  • D) WebRTC only works with text

Hint: a different transport stack determines the transmission format.

57. How is the WebSocket protocol denoted in a URL?

  • A) http:// https://
  • B) ws:// wss:// *
  • C) ftp:// ftps://
  • D) tcp:// tcps://

Hint: it's a short abbreviation of the technology's name.

58. Which prefix is used for a secure WebSocket connection?

  • A) https://
  • B) wss:// *
  • C) ssl://
  • D) secure://

Hint: the analog of https, but for WebSocket.

59. How is a WebRTC connection denoted in a URL?

  • A) webrtc://
  • B) rtc://
  • C) It has no standard URL protocol *
  • D) p2p://

Hint: the connection is established not via a direct URL, but via signaling.

60. What is the core essence of the WebSocket Pub/Sub model?

  • A) The client calls a specific method on the server
  • B) Clients subscribe to channels and receive events *
  • C) All data goes through SQL
  • D) Only HTTP polling is used

Hint: there's a "subscription" and a "publication" of events.

61. How does WebSocket RPC differ from Pub/Sub?

  • A) RPC is a subscription to events and cannot be used together with Pub/Sub
  • B) Pub/Sub is a direct function call and cannot be used together with RPC
  • C) RPC is request-response, Pub/Sub is event distribution *
  • D) There's no difference, they're just different names for the same protocol

Hint: one is similar to an API call, the other — to a broadcast.

62. Which approach (architecture) is better suited for chats and notifications?

  • A) RPC
  • B) FTP
  • C) Pub/Sub *
  • D) SOAP

Hint: the client subscribes to the channels it needs, and then messages are distributed immediately to many corresponding subscribers.

63. What is the key difference in implementing private channels versus public ones in Laravel Echo?

  • A) Private channels don't use WebSocket
  • B) Public channels require authorization
  • C) Private channels require a server-side access check in channel.php*
  • D) Public channels use Redis, private ones don't

Hint: a check of the user's rights is performed before subscribing.

64. What's the main difference between public channels and private ones?

  • A) on the client side a public channel is declared as window.Echo.private('chat.1').listen('MessageSent', (e) => { ... });
  • B) For open events without access restrictions * compared to private channels
  • C) a public channel is used only for personal messages
  • D) Public channels require authorization via /broadcasting/auth and a check of the user's rights.

Hint: any client can subscribe without a check.

64. How is subscribing to private channels done in Laravel Echo?

  • A) Subscribing to private and public channels is done identically on the client side, the difference is only on the backend.
  • B) window.Echo.channel('chat').listen('MessageSent', (e) => { ... });
  • C) window.Echo.private('chat.1').listen('MessageSent', (e) => { ... }); *
  • D) For private channels it's enough to call window.Echo.join('chat'), no authorization is required.

Hint: Recall which channels require an access check via Broadcast::channel() and the /broadcasting/auth route, and what particular feature exists on the client side.

65. What does Laravel Echo do «behind the scenes» when subscribing to a private channel, if the programmer declares and calls window.Echo.private('chat.1').listen('MessageSent', (e) => { ... });?

  • A) It automatically creates a new channel in the database with the ID of the corresponding users who have access to the chats based on Broadcast::channel() in routes/channels.php
  • B) It sends a request to /broadcasting/auth to check access rights. * and the server checks access rights via Broadcast::channel() in routes/channels.php
  • C) It saves all events to the browser's local storage, and checks access on receiving each event
  • D) It connects directly to the private channel without authorization.

Hint: Think about how Laravel Echo finds out whether the user has the right to connect to a private channel. What happens between the client and the server before the subscription becomes possible?

See also

  • [[b9165]]
  • [[7016]]
  • [[b14139]]

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)