pusher/backpusher — Issue #3: The socket_id is undefined if Backpusher is instantiated after connecting to Pusher
Author: monicao
Created: 2012-01-26 19:28:08+00:00
State: open

My app has two different kinds of backbone collections that are connected to pusher (People and Documents). They each use their own pusher channels, so I placed the code that creates the channel for the collection in the collection's initializer.

``` coffeescript
class Warp.Collections.Documents extends Backbone.Collection
initialize: ->
console.log "Initializing Warp.Collections.Documents"

channel = Warp.pusher.subscribe("documents-#{Warp.Config.meeting_token}")
backpusher = new Backpusher(channel, this)
# other code...
```

Warp.pusher (the pusher instance) is created in the function that starts the Backbone app

```coffeescript

window.Warp =
Models: {}
Collections: {}
Views: {}
Routers: {}
Config: {}
init: (options = {}) ->
console.log "Initializing app. "
_.extend(@Config, options);

@pusher = new Pusher(@Config.pusher_key)

new Warp.Routers.Main()
unless Backbone.history.started
Backbone.history.start()
Backbone.history.started = true
```

The problem is that the Pusher connection is already established when backpusher is instantiated. Backpusher binds to the Pusher connections "connected" event to set the socket_id, but that event never fires. Consequently, the socket_id remains undefined.

Am I using Backpusher the wrong way or would it make sense to add some code that checks if the pusher connection is already established and fetch the socket id right away? (see pull request -- coming up soon)