From cf94ce42099612a62596f5a6ac1055cfa352d6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Blanco?= Date: Wed, 28 Jul 2021 16:02:46 +0200 Subject: [PATCH 1/2] WIP --- broadcasting.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/broadcasting.md b/broadcasting.md index 836073b5a51..61c47690a97 100644 --- a/broadcasting.md +++ b/broadcasting.md @@ -22,6 +22,7 @@ - [Defining Authorization Callbacks](#defining-authorization-callbacks) - [Defining Channel Classes](#defining-channel-classes) - [Broadcasting Events](#broadcasting-events) + - [Customizing The Connection](#customizing-the-connection) - [Only To Others](#only-to-others) - [Receiving Broadcasts](#receiving-broadcasts) - [Listening For Events](#listening-for-events) @@ -613,6 +614,41 @@ Once you have defined an event and marked it with the `ShouldBroadcast` interfac OrderShipmentStatusUpdated::dispatch($order); + +### Customizing The Connection + +If your application need to interact with multiple broadcast connections and you want to use a different broadcaster than your default one, you may specify which connection to push an event to using the `via` method: + + use App\Events\OrderShipmentStatusUpdated; + + broadcast(new OrderShipmentStatusUpdated($update))->via('pusher'); + +Alternatively, you may specify the event's broadcast connection by calling the `broadcastVia` method within the event's constructor: + + broadcastVia('pusher'); + } + } + ### Only To Others From 83ffd9644993a32ea7850ab6898018d2e8f33f90 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Jul 2021 08:10:18 -0500 Subject: [PATCH 2/2] formatting --- broadcasting.md | 62 ++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/broadcasting.md b/broadcasting.md index 61c47690a97..f5e6afa3984 100644 --- a/broadcasting.md +++ b/broadcasting.md @@ -22,8 +22,8 @@ - [Defining Authorization Callbacks](#defining-authorization-callbacks) - [Defining Channel Classes](#defining-channel-classes) - [Broadcasting Events](#broadcasting-events) - - [Customizing The Connection](#customizing-the-connection) - [Only To Others](#only-to-others) + - [Customizing The Connection](#customizing-the-connection) - [Receiving Broadcasts](#receiving-broadcasts) - [Listening For Events](#listening-for-events) - [Leaving A Channel](#leaving-a-channel) @@ -614,10 +614,39 @@ Once you have defined an event and marked it with the `ShouldBroadcast` interfac OrderShipmentStatusUpdated::dispatch($order); + +### Only To Others + +When building an application that utilizes event broadcasting, you may occasionally need to broadcast an event to all subscribers to a given channel except for the current user. You may accomplish this using the `broadcast` helper and the `toOthers` method: + + use App\Events\OrderShipmentStatusUpdated; + + broadcast(new OrderShipmentStatusUpdated($update))->toOthers(); + +To better understand when you may want to use the `toOthers` method, let's imagine a task list application where a user may create a new task by entering a task name. To create a task, your application might make a request to a `/task` URL which broadcasts the task's creation and returns a JSON representation of the new task. When your JavaScript application receives the response from the end-point, it might directly insert the new task into its task list like so: + + axios.post('/task', task) + .then((response) => { + this.tasks.push(response.data); + }); + +However, remember that we also broadcast the task's creation. If your JavaScript application is also listening for this event in order to add tasks to the task list, you will have duplicate tasks in your list: one from the end-point and one from the broadcast. You may solve this by using the `toOthers` method to instruct the broadcaster to not broadcast the event to the current user. + +> {note} Your event must use the `Illuminate\Broadcasting\InteractsWithSockets` trait in order to call the `toOthers` method. + + +#### Configuration + +When you initialize a Laravel Echo instance, a socket ID is assigned to the connection. If you are using a global [Axios](https://github.com/mzabriskie/axios) instance to make HTTP requests from your JavaScript application, the socket ID will automatically be attached to every outgoing request as a `X-Socket-ID` header. Then, when you call the `toOthers` method, Laravel will extract the socket ID from the header and instruct the broadcaster to not broadcast to any connections with that socket ID. + +If you are not using a global Axios instance, you will need to manually configure your JavaScript application to send the `X-Socket-ID` header with all outgoing requests. You may retrieve the socket ID using the `Echo.socketId` method: + + var socketId = Echo.socketId(); + ### Customizing The Connection -If your application need to interact with multiple broadcast connections and you want to use a different broadcaster than your default one, you may specify which connection to push an event to using the `via` method: +If your application interacts with multiple broadcast connections and you want to broadcast an event using a broadcaster other than your default, you may specify which connection to push an event to using the `via` method: use App\Events\OrderShipmentStatusUpdated; @@ -649,35 +678,6 @@ Alternatively, you may specify the event's broadcast connection by calling the ` } } - -### Only To Others - -When building an application that utilizes event broadcasting, you may occasionally need to broadcast an event to all subscribers to a given channel except for the current user. You may accomplish this using the `broadcast` helper and the `toOthers` method: - - use App\Events\OrderShipmentStatusUpdated; - - broadcast(new OrderShipmentStatusUpdated($update))->toOthers(); - -To better understand when you may want to use the `toOthers` method, let's imagine a task list application where a user may create a new task by entering a task name. To create a task, your application might make a request to a `/task` URL which broadcasts the task's creation and returns a JSON representation of the new task. When your JavaScript application receives the response from the end-point, it might directly insert the new task into its task list like so: - - axios.post('/task', task) - .then((response) => { - this.tasks.push(response.data); - }); - -However, remember that we also broadcast the task's creation. If your JavaScript application is also listening for this event in order to add tasks to the task list, you will have duplicate tasks in your list: one from the end-point and one from the broadcast. You may solve this by using the `toOthers` method to instruct the broadcaster to not broadcast the event to the current user. - -> {note} Your event must use the `Illuminate\Broadcasting\InteractsWithSockets` trait in order to call the `toOthers` method. - - -#### Configuration - -When you initialize a Laravel Echo instance, a socket ID is assigned to the connection. If you are using a global [Axios](https://github.com/mzabriskie/axios) instance to make HTTP requests from your JavaScript application, the socket ID will automatically be attached to every outgoing request as a `X-Socket-ID` header. Then, when you call the `toOthers` method, Laravel will extract the socket ID from the header and instruct the broadcaster to not broadcast to any connections with that socket ID. - -If you are not using a global Axios instance, you will need to manually configure your JavaScript application to send the `X-Socket-ID` header with all outgoing requests. You may retrieve the socket ID using the `Echo.socketId` method: - - var socketId = Echo.socketId(); - ## Receiving Broadcasts