Pusher - Private channel subscription
javascript, php, pusher
Solution
You should set up a route for the Pusher authentication `Route::post('pusher/auth', 'ApiController@pusherAuth');`
In that method you should first disable php debugbar (if you're using it) authenticate the user and if authentication checks, then return the response.
I'll paste my controller code below.
public function pusherAuth()
{
\Debugbar::disable();
$user = auth()->user();
if ($user) {
$pusher = new \Pusher(config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'));
echo $pusher->socket_auth(request()->input('channel_name'), request()->input('socket_id'));
return;
}else {
header('', true, 403);
echo "Forbidden";
return;
}
}
My JS code:
var pusher = new Pusher(project.pusherKey, {
cluster: 'eu',
encrypted: true,
authEndpoint: apiUrl(['pusher', 'auth']), // just a helper method to create a link
auth: {
headers: {
'X-CSRF-Token': project.token // CSRF token
}
}
});
var channelName = 'private-notifications-' + project.userId; // channel for the user id
var channel = pusher.subscribe(channelName);
channel.bind('new_notification', function (data)
{
app.addNotification(data); // send the notification in the JS app
});
I hope this helps.
Cheers!
Problem
I have a code with subscribe private channels, and when I try make a subscription I have the next message: Pusher : Couldn't get auth info from your webapp : 404 Scenario: Javascript(Sencha touch) and PHP(Laravel) The subscription is in javascript: ``` Pusher.channel_auth_endpoint = "/pusher.php"; var APP_KEY = '4324523452435234523'; var pusher = new Pusher(APP_KEY); var channel = pusher.subscribe('private-l2'); channel.bind('pusher:subscription_succeeded', function() { alert("ahora siiii"); }); // for debugging purposes. Not required. Pusher.log = function(msg) { if(window.console && window.console.log) { window.console.log("PUSHER LOG: "+msg); } } ``` AND the pusher.php / LARAVEL ``` $this->app_id = '66981'; $this->app_key = '4324523452435234523'; $this->app_secret = 'f34632459911e2670dcf'; $pusher = new Pusher($this->app_key, $this->app_secret, $this->app_id); $auth = $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id')); echo $auth; ``` The result is the error: ``` Pusher : State changed : connecting -> connected Pusher : Couldn't get auth info from your webapp : 404 ```