Pusher subscribe and un-subscribe process

2.2k views Asked by At

I'm getting a Pusher subscribe/un-subscribe problem with presence groups on a fairly simple chat application. There should be two subscribed channels at a given time. Users switch between channels when navigating between backbone routes, so there are no hard page reloads.

Pusher appears to function most of the time, but I get intermittent errors for channel subscriptions.

I wrote two join channel methods that unsubscribe if one has been joined by a previous route. I'm worried that there is something async happening within Pusher that is breaking things.

My pusher related code for a single channel:

window.pusher = new Pusher('<%= Pusher.key %>', {
  authEndpoint: 'api/pusher/auth'
});

Route:

this.groupFeed = this._pusherSubscribeGroup(group_id);
this.groupFeed.bind('new_conversation', function(data) {
  var newConv = new App.Models.Conversation(data);
  this.group.conversations().add(newConv);
}.bind(this));

Unsubscribe helper:

_pusherSubscribeGroup: function (group_id) {
  if (this._groupChannelName) {
    window.pusher.unsubscribe(this._groupChannelName);
  }
  this._groupChannelName = 'presence-group-' + group_id;
  return window.pusher.subscribe(this._groupChannelName);
}

Console error:

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Existing subscription to channel presence-group-1"}}}
1

There are 1 answers

0
leggetter On

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Existing subscription to channel presence-group-1"}}}

The message is telling you that - as far as the Pusher service is concerned - this particular client identified by a socket_id is already subscribed to the presence-group-1 channel. From the point of view of the client this error normally isn't something you actually need to worry about.

However, if the user is quickly subscribing/unsubscribing by navigating routes it would be worth getting some more information to be completely sure. From the supplied code and description, it's not possible to determine if the problem is within the Pusher service or the client application.

Providing the output of the Pusher JavaScript library logging will provide more detail that could help determine the cause.