Class: AnyCable::BroadcastAdapters::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/anycable/broadcast_adapters/redis.rb

Overview

Redis adapter for broadcasting.

Example:

AnyCable.broadast_adapter = :redis

It uses Redis configuration from global AnyCable config by default.

You can override these params:

AnyCable.broadcast_adapter = :redis, url: "redis://my_redis", channel: "_any_cable_"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel: AnyCable.config.redis_channel, **options) ⇒ Redis

Returns a new instance of Redis.



25
26
27
28
29
30
31
32
# File 'lib/anycable/broadcast_adapters/redis.rb', line 25

def initialize(
  channel: AnyCable.config.redis_channel,
  **options
)
  options = AnyCable.config.to_redis_params.merge(options)
  @redis_conn = ::Redis.new(options)
  @channel = channel
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



23
24
25
# File 'lib/anycable/broadcast_adapters/redis.rb', line 23

def channel
  @channel
end

#redis_connObject (readonly)

Returns the value of attribute redis_conn.



23
24
25
# File 'lib/anycable/broadcast_adapters/redis.rb', line 23

def redis_conn
  @redis_conn
end

Instance Method Details

#broadcast(stream, payload) ⇒ Object



34
35
36
37
38
39
# File 'lib/anycable/broadcast_adapters/redis.rb', line 34

def broadcast(stream, payload)
  redis_conn.publish(
    channel,
    { stream: stream, data: payload }.to_json
  )
end