Question

In my rails app a User has a profile and this profile can be publicly accessible. I have been looking at using the Pusher gem (https://github.com/pusher/pusher-gem) to use for plug-and-play websocket usage with my app.

Basically I am wanting it so that if a user is looking at a public profile and the owner of that profile happens to update that profile then the front-end is updated with the new information and the user is notified on the front-end about the update.

Any help on where to get started would be great!

Was it helpful?

Solution

For each public profile have a unique channel name e.g. <user_name>-profile.

Whenever an updated occurs on the user user_name's profile trigger an event on the user's channel, passing the updated data.

data = update_profile()
Pusher.trigger( '<user_name>-profile', 'profile-updated', {:profile => data} )

On the profile page running the the browser have the code that listens to updates only on the relevant channel:

var pusher = new Pusher( APP_KEY );
var channel = pusher.subscribe( '<user_name>-profile' );
channel.bind( 'profile-updated', function( update ) {
  // Update UI to show new profile information
  // Show something to indicate that an update has occurred
} );

The one problem here is that you will be triggering an event even when nobody is viewing the public profile. If you wanted to fix that you would need to use WebHooks and keep track of whether or not a profile channel was occupied and only trigger the event if it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top