Ian
Ian

Reputation: 4185

Provide my users with a realtime API using Pusher

I'm using Pusher to add realtime functionality to my app. Is there a way to provide my users with realtime functionality through an API? I'm using private and presence channels, so connections to these need to be authenticated. Has anyone worked with Pusher and provided some sort of API to their users?

I'm doing this using Rails 3.1.

Upvotes: 0

Views: 455

Answers (1)

leggetter
leggetter

Reputation: 15467

The solution here is to give the users you want to be able to access your data your app_key (not app_secret). They can then connect to Pusher and try to subscribe to your channels. They'll need to use JSONP authentication which makes a call to your server where you can authenticate the request to the private or presence channels.

Pusher.channel_auth_endpoint = 'http://yourserver.com/pusher_jsonp_auth';
Pusher.channel_auth_transport = 'jsonp';
var pusher = new Pusher('YOUR_APP_KEY');
var channel = pusher.subscribe('private-your-channel');
channel.bind('your_event', function(data) {
  // do something here with data
});

In your authentication you'll need to check the referrer (domain) to see if you've given them access to your data along with what they are subscribing to.

You could also wrap this JavaScript up in your own library so that a subscription_error (authentication error) disconnects the client from Pusher.

Hope this helps. You can always drop an email to [email protected] too.

Upvotes: 2

Related Questions