Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pusher - Private channel subscription

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 
like image 240
user2842722 Avatar asked May 26 '14 11:05

user2842722


People also ask

How many channels can I create on Pusher?

Each application can have one channel or many, and each client can choose which channels it subscribes to.

Is pusher free to use?

What is Pusher? Pusher enables real-time experiences for mobile and web applications. It has two features that are the real-time API and Push-Notifications. A free tier is available, and pricing starts at $49/month.

What is a channel in pusher?

Pusher Channels provides realtime communication between servers, apps and devices. Channels is used for realtime charts, realtime user lists, realtime maps, multiplayer gaming, and many other kinds of UI updates. When something happens in your system, it can update web-pages, apps and devices.


2 Answers

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!

like image 125
adrianthedev Avatar answered Sep 25 '22 00:09

adrianthedev


Private Pusher channels require the client to authenticate for access. See http://pusher.com/docs/authenticating_users for details on configuring the client for authentication and setting up an authentication endpoint.

like image 31
Daryl Spitzer Avatar answered Sep 22 '22 00:09

Daryl Spitzer