Question

I didn't find anything useful on the internet about this, so I want to share.

Was it helpful?

Solution

On Client:

var pusher = new Pusher('your-app-key', {authTransport: 'parse'});

Pusher.authorizers.parse = function (socketId, callback) {
    var pusherData = { 
        socket_id: socketId, 
        channel_name: this.channel.name 
    };
    Parse.Cloud.run('authorizePusherChannel', pusherData, {
        success: function (result) {
            callback(false, JSON.parse(result));
        },
        error: function (error) {
            callback(true, error);
        }
    });
};

On Parse Cloud:

Parse.Cloud.define('authorizePusherChannel', function (request, response) {
    if (!request.user) { response.error('User should be autenticated.'); }
    var pusherAppKey = 'your-pusher-app-key';
    var pusherAppSecret = 'your-pusher-app-secret';
    var stringToSign = request.params.socket_id + ':' + request.params.channel_name;
    var authToken = pusherAppKey + ':' + crypto.createHmac('sha256', pusherAppSecret).update(stringToSign).    digest('hex');    
    response.success(JSON.stringify({auth:authToken}));
});

OTHER TIPS

Pusher.authorizers.parse = function (socketId, callback) {
    var pusherData = { 
        socket_id: socketId, 
        channel_name: this.channel.name 
    };
    Parse.Cloud.run('authorizePusherChannel', pusherData, {
        success: function (result) {
            callback(false, JSON.parse(result));
        },
        error: function (error) {
            callback(true, error);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top