Skip to content

Instantly share code, notes, and snippets.

@blueridgemountains1
Last active August 14, 2017 16:11
Show Gist options
  • Save blueridgemountains1/f9a32887ffbe3d3da0a75b087dc5593c to your computer and use it in GitHub Desktop.
Save blueridgemountains1/f9a32887ffbe3d3da0a75b087dc5593c to your computer and use it in GitHub Desktop.
Add Pusher to Laravel
  1. Add pusher PHP server to composer
"pusher/pusher-php-server": "^3.0",
  1. Require this package, with Composer, in the root directory of your project.
$ composer require vinkla/pusher
  1. Add the service provider to config/app.php in the providers array, or if you're using Laravel 5.5, this can be done via the automatic package discovery.
Vinkla\Pusher\PusherServiceProvider::class
  1. If you want you can use the facade. Add the reference in config/app.php to your aliases array.
'Pusher' => Vinkla\Pusher\Facades\Pusher::class
  1. Laravel Pusher requires connection configuration. To get started, you'll need to publish all vendor assets:
$ php artisan vendor:publish
  1. Add pusher info to .env file
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=
  1. Add code
Pusher::trigger('my-channel', 'my-event', ['message' => $message]);
// We're done here - how easy was that, it just works!

Pusher::getSettings();
// This example is simple and there are far more methods available.
  1. Add to laravel bootstrap Js file. May already be there.
window.Pusher = require('pusher-js');
  1. Add to script at header to include the variables for JS. That way if anything changes on dev or prod, you don't have to worry. Similar to wp_localize_script
const LaravelVars = {
    "pusher_key": "{{ env("PUSHER_APP_KEY") }}",
    "pusher_cluster": "{{ env("PUSHER_APP_CLUSTER") }}",
}
  1. Add pusher info to app.js file
const socket = new Pusher(LaravelVars.pusher_key, {
   cluster: LaravelVars.pusher_cluster,
});

const channel = socket.subscribe('my-channel');

channel.bind('my-event',function(data) {
    alert("commented!");
   // or do whatever you need in Vue or jQuery, etc.
});
  1. Make sure you run the script update
yarn run dev
// or npm run dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment