mardi 23 février 2016

Creating private channel for user on login with pusher

I am attempting to implement pusher into a laravel aplication. Specifically I want to create a private channel when the user logs in.

I have a public channel "login" that has an event called "login-event" that gets triggered every time someone logs in.

The issue I am having is that the trigger that is supposed to create and subscribe to the private channel only fires for the users already logged in. not for the user that just logged in, which is the only user I actually need it to run for.

Pusher.js

$(function () {

  var pusher = new Pusher('XXXXXXXXXX');
  var channel = pusher.subscribe('login-channel');

  channel.bind('login-event', function(data) {


 // NOT RUNNING FOR USER WHO JUST LOGGED IN BUT RUNS FOR EVERYONE ELSE

  var user = data.content.userID;

  var newpusher = new Pusher('XXXXXXXXXXX', {
    userid: $('meta[name="user"]').attr('content'),
    authEndpoint: '/pusher/auth',
    auth: {
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    }
});

  var newchannel = newpusher.subscribe('private-channel-for-'+user+'');

  console.log('Setting up a channel for user '+user+'.');

});

I need to "login-event' in the code above to also run for the user that just logged in.

I have looked over the docs about as much as I can take, if the answer is clearly in there feel free to provide a link but it would be great if I could get a suggestion in code.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire