code

How To Setup Your Rails Backend For Pusher

Yashu Mittal

Pusher.js is ready to use SaaS providing websockets in multiple technologies It may handle public, private and client events with authorization or not.

What you will use:

Pros:

Cons:

Paid service (check out pricing here)

Step 1: Setup initializer

Pusher will be available as global singleton object being set up in initializer. You need to add pusher.rb to your initializers that will simply define app credentials and enable encryption.

Step 2: Create Pusher::Dispatcher service to trigger events

This step depends on your architecture of choice. I chose to have Pusher::Dispatcher class that will just trigger the Pusher singleton. Looks like overkill, but it’s a good place to e.g. calculate channel name based on some variables (like user id).

Step 3: Add authorization endpoint

Authorization in Pusher is very simple:

Each client can define its authorization endpoint during initialization. E.g. in frontend app or iOS app you can initialize Pusher with authorization endpoint pointed out to //myapp.com/api/pusher/auth.

Clients can also configure params that they are sending to that endpoint. E.g. you can send to your /api/pusher/auth endpoint current user ID and auth token that is being used in frontend application.

Let’s call our endpoint /api/pusher/auth.

Safe, easy and secure.

Step 4: Debug and validate

You can easily debug and validate your Pusher implementation without any frontend at all. In my app, where there is basically no business logic, I created simple endpoint that will trigger Pusher events on demand for testing.

Now I can simply trigger Pusher events by using CURL:

curl http://localhost:3000/api/pusher/trigger?channel_name=test&event=myEvent

What is cool about Pusher is that it has excellent debug console available on web. What it means is that you don’t need to have frontend ready solution - work on backend till you’re ready!

Go to your’s app Debug Console in Pusher dashboard, restart your server and observe if Pusher is getting needed data!

Summary

You have set up all you need to fully embrace Pusher base possibilities. You can handle public, private and presence channel (private and presence demands authorization). You can easily trigger events and you have reusable Pusher::Dispatcher class. And you learnt about Pusher Debug console which is must-have in your toolbelt. Good job!

Response to “How To Setup Your Rails Backend For Pusher”

Stay current

Sign up for our newsletter, and we'll send you news and tutorials on business, growth, web design, coding and more!