Sitemap
Cristian Tountzis

Cristian Tountzis

Tracking Signed In Users With Ember.js and Pusher Presence Channels

2 min readOct 24, 2014

--

It turns out keeping track of who is signed in to your application is pretty easy using Pusher Presence Channels.

Not having played around with Pusher much, my first instinct was to override our devise sessions controllers to write to the database when a user signed in our out. Not the best solution in the world.

A better and more flexible solution is to use Pusher presence channels. Pusher presence channels extend on private channel security, and give you an awareness of who is subscribed to that channel (you can check the docs here: http://pusher.com/docs/client_api_guide/client_presence_channels)

I’m not going to jump into the rails side, but we use the pusher-gem (https://github.com/pusher/pusher-gem) to handle that. To get started, we need to set our channel in ember -

@set ‘presenceChannel’, pusher.subscribe(@get(‘presenceChannelName’))

and then bind three of the presence channel events to some functions-

@get(‘presenceChannel’).bind ‘pusher:subscription_succeeded’, @handlePresenceChannelSubscription.bind(this)

@get(‘presenceChannel’).bind ‘pusher:member_added’, @handlePresenceChannelMemberAdded.bind(this)

@get(‘presenceChannel’).bind ‘pusher:member_removed’, @handlePresenceChannelMemberRemoved.bind(this)

pusher:subscription_succeeded is called when your presence channel is subscribed to, so we can use it to store an array of member’s ids who are online -

Get Cristian Tountzis’s stories in your inbox

Join Medium for free to get updates from this writer.

handlePresenceChannelSubscription: (members) -> userIds = [] members.each (m) -> userIds.push(+m.id) @set ‘onlineUserIds’, userIds

pusher:member_added gets called when a user joins the channel. This only gets triggered the first time a member joins, so opening multiple tabs or signing in twice from different browsers won’t matter. In this callback we can push the member object onto our list of members -

handlePresenceChannelMemberAdded: (member) -> members = @get ‘onlineUserIds’ members.pushObject +member.id

pusher:member_removed gets called when a user leaves the channel, so we can remove the member from the members list in this callback.

handlePresenceChannelMemberRemoved: (member) -> members = @get ‘onlineUserIds’ members.removeObject +member.id

So now we have an list of members who are online all handled by pusher in realtime. So lets use it in our app. Lets add a function to the streaming service to return the list of online users.

onlineUsers: (-> store = @get ‘store’ @get(‘onlineUserIds’).map (id) -> store.getById ‘user’, id ).property ‘onlineUserIds.@each’

Then we need to inject the streaming service in an initialiser so it is available in our controller.

application.inject ‘controller’, ‘streaming’, ‘streaming:service’

Now we can can use a computed property to check if a user is online in the controller.

isSignedIn: (-> users = @get(‘streaming.onlineUsers’) users.contains @get(‘model’) ).property ‘streaming.onlineUsers.@each’

And that’s it. Call that method in your template to see if a user is signed in. Right now we use this show online/offline status, but with a bit of tweaking it can be used for things like chat rooms, online games or document collaboration.

Big thanks to @itscatkins for pairing with me for this one, first time playing with Pusher went pretty sweet thanks to your help bud!

--

--