Sha256: 20a9b4c6ec17e2bfe296dde874e287056f9cb59cc703f3faea6c0f22932fc430

Contents?: true

Size: 936 Bytes

Versions: 3

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

gem "redis", ">= 3"

require "redis"
require "json"

module AnyCable
  module BroadcastAdapters
    # 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_"
    class Redis
      attr_reader :redis_conn, :channel

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
anycable-0.6.5 lib/anycable/broadcast_adapters/redis.rb
anycable-0.6.4 lib/anycable/broadcast_adapters/redis.rb
anycable-0.6.3 lib/anycable/broadcast_adapters/redis.rb