Skip to content

Instantly share code, notes, and snippets.

@caporro
Last active May 26, 2019 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caporro/08453e8a65a2f0c8fe56c67e122984c0 to your computer and use it in GitHub Desktop.
Save caporro/08453e8a65a2f0c8fe56c67e122984c0 to your computer and use it in GitHub Desktop.
Laravel, Broadcast Event, websoket, socket.io, redis queue, private channel, authentication

npm install -g laravel-echo-server sudo apt-get install redis composer require predis/predis

laravel-echo-server init

set the file /etc/hosts 127.0.0.1 mysite.com

laravel-echo-server start

$ npm install --save socket.io-client

$ npm install --save laravel-echo

php artisan queue:listen --tries=1

make new event class php artisan make:event NameEvent implements ShouldBroadcast in rbroadcastOn() edit the private channel return new PrivateChannel('cname.'.'SVUX2U6K');

add in resources/js/bootstrap.js

import Echo from 'laravel-echo'

window.io = require('socket.io-client'); window.Echo = new Echo({ broadcaster: 'socket.io', host: window.location.hostname + ':6001' }); and after run compile js comand: npm run dev

in client add:

<script src="{{ mix('js/app.js') }}"></script>

and:

<script> //private channel window.Echo.private('cname.SVUX2U6K') .listen('NameEvent', (e) => { console.log( e); }); </script>

test run new broadcast event from get request Route::get('t', function(){ broadcast(new \App\Events\NameEvent); //event(new \App\Events\NameEvent);

});

routes/channel.php add auth method: Broadcast::channel('cname.{code}', function() { if ($codeuser == $code) return true; else return false; });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment