Class: Vines::Cluster::Publisher

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/vines/cluster/publisher.rb

Overview

Broadcast messages to other cluster nodes via redis pubsub channels. All members subscribe to a channel for heartbeats, online, and offline messages from other nodes. This allows new nodes to be added to the cluster dynamically, without configuring all other nodes.

Instance Method Summary collapse

Methods included from Log

#log, set_log_file

Constructor Details

#initialize(cluster) ⇒ Publisher

Returns a new instance of Publisher.



14
15
16
# File 'lib/vines/cluster/publisher.rb', line 14

def initialize(cluster)
  @cluster = cluster
end

Instance Method Details

#broadcast(type) ⇒ Object

Publish a :heartbeat, :online, or :offline message to the nodes:all broadcast channel.



20
21
22
23
24
25
26
# File 'lib/vines/cluster/publisher.rb', line 20

def broadcast(type)
  redis.publish(ALL, {
    from: @cluster.id,
    type: type,
    time: Time.now.to_i
  }.to_json)
end

#redisObject



50
51
52
# File 'lib/vines/cluster/publisher.rb', line 50

def redis
  @cluster.connection
end

#route(stanza, node) ⇒ Object

Send the stanza to the node hosting the user’s session. The stanza is published to the channel to which the remote node is listening for messages.



31
32
33
34
35
36
37
38
# File 'lib/vines/cluster/publisher.rb', line 31

def route(stanza, node)
  log.debug { "Sent cluster stanza: %s -> %s\n%s\n" % [@cluster.id, node, stanza] }
  redis.publish("cluster:nodes:#{node}", {
    from: @cluster.id,
    type: STANZA,
    stanza: stanza.to_s
  }.to_json)
end

#update_user(jid, node) ⇒ Object

Notify the remote node that the user’s roster has changed and it should reload the user from storage.



42
43
44
45
46
47
48
# File 'lib/vines/cluster/publisher.rb', line 42

def update_user(jid, node)
  redis.publish("cluster:nodes:#{node}", {
    from: @cluster.id,
    type: USER,
    jid: jid.to_s
  }.to_json)
end