Last active
March 18, 2025 12:15
-
-
Save moaalaa/2dd3cdfc46f6ea1bddf322af73cb973f to your computer and use it in GitHub Desktop.
Realtime work around
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace MixCode\Providers; | |
| use Illuminate\Support\ServiceProvider; | |
| use Illuminate\Support\Facades\Broadcast; | |
| class BroadcastServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Bootstrap any application services. | |
| * | |
| * @return void | |
| */ | |
| public function boot() | |
| { | |
| if (request()->hasHeader('authorization')){ | |
| Broadcast::routes(['middleware' => 'auth:api']); | |
| } else { | |
| Broadcast::routes(); | |
| } | |
| require base_path('routes/channels.php'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Echo.connector.socket.on("connect", () => { | |
| console.log("Connected"); | |
| }); | |
| // Listen to sender incomming messages | |
| Echo.private(`new-message.${this.sender.id}`).listen( | |
| ".new_chat_message", | |
| e => { | |
| console.log(e); | |
| this.messages.push({ | |
| message: e.message, | |
| sender: e.sender, | |
| receiver: e.receiver, | |
| is_sender: false, | |
| is_receiver: true | |
| }); | |
| } | |
| ); | |
| Echo.connector.socket.on("disconnect", () => { | |
| console.log("Disconnected"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://github.com/tlaverdure/laravel-echo-server/issues/266#issuecomment-365599129 | |
| https://stackoverflow.com/questions/48934892/laravel-broadcast-combining-multiple-middleware-web-authapi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var config = { | |
| auth: { | |
| headers: { | |
| // "X-CSRF-TOKEN": document.head.querySelector('meta[name="csrf-token"]').content, | |
| "Authorization": "Bearer TOKEN" | |
| } | |
| }, | |
| host: "127.0.0.1:6001" | |
| }; | |
| var socket = io(config.host, config); | |
| console.log(socket); | |
| socket.on("connect", () => { | |
| console.log("Connected"); | |
| }); | |
| socket.on("disconnect", () => { | |
| console.log("Disconnected"); | |
| }); | |
| // Socket channel subscription | |
| const subscribe = () => { | |
| socket.emit("subscribe", { | |
| channel: `private-new-message.${this.sender.id}`, | |
| auth: config.auth | |
| }); | |
| }; | |
| subscribe(); | |
| // Reconnecting your channel "private-" using laravel echo server 'reconnect' channel. | |
| socket.on("reconnect", subscribe); | |
| // Socket.on will return channel name and the data "channel event name with out '.' before it" | |
| socket.on("new_chat_message", (channel, e) => { | |
| console.log(channel, e); | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment