aboutsummaryrefslogtreecommitdiffstats
path: root/lib/assets/javascripts/cable/consumer.js.coffee
blob: b01ae586aabd91f70849e0861f88097f7a635f89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#= require cable/connection
#= require cable/connection_monitor
#= require cable/subscriptions
#= require cable/subscription

# The Cable.Consumer establishes the connection to a server-side Ruby Connection object. Once established, 
# the Cable.ConnectionMonitor will ensure that its properly maintained through heartbeats and checking for stale updates.
# The Consumer instance is also the gateway to establishing subscriptions to desired channels through the #createSubscription
# method.
#
# The following example shows how this can be setup:
#
#   @App = {}
#   App.cable = Cable.createConsumer "http://example.com/accounts/1"
#   App.appearance = App.cable.createSubscription "AppearanceChannel"
#
# For more details on how you'd configure an actual channel subscription, see Cable.Subscription.
class Cable.Consumer
  constructor: (@url) ->
    @subscriptions = new Cable.Subscriptions this
    @connection = new Cable.Connection this
    @connectionMonitor = new Cable.ConnectionMonitor this

  createSubscription: (channelName, mixin) ->
    channel = channelName
    params = if typeof channel is "object" then channel else {channel}
    new Cable.Subscription this, params, mixin

  send: (data) ->
    @connection.send(data)

  inspect: ->
    JSON.stringify(this, null, 2)

  toJSON: ->
    {@url, @subscriptions, @connection, @connectionMonitor}