Class: ActionCable::SubscriptionAdapter::PostgreSQL

Inherits:
Base
  • Object
show all
Defined in:
lib/action_cable/subscription_adapter/postgresql.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Listener

Instance Attribute Summary

Attributes inherited from Base

#logger, #server

Instance Method Summary collapse

Constructor Details

#initializePostgreSQL

Returns a new instance of PostgreSQL.



11
12
13
14
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 11

def initialize(*)
  super
  @listener = nil
end

Instance Method Details

#broadcast(channel, payload) ⇒ Object



16
17
18
19
20
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 16

def broadcast(channel, payload)
  with_connection do |pg_conn|
    pg_conn.exec("NOTIFY #{pg_conn.escape_identifier(channel_identifier(channel))}, '#{pg_conn.escape_string(payload)}'")
  end
end

#shutdownObject



30
31
32
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 30

def shutdown
  listener.shutdown
end

#subscribe(channel, callback, success_callback = nil) ⇒ Object



22
23
24
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 22

def subscribe(channel, callback, success_callback = nil)
  listener.add_subscriber(channel_identifier(channel), callback, success_callback)
end

#unsubscribe(channel, callback) ⇒ Object



26
27
28
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 26

def unsubscribe(channel, callback)
  listener.remove_subscriber(channel_identifier(channel), callback)
end

#with_connection(&block) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 34

def with_connection(&block) # :nodoc:
  ActiveRecord::Base.connection_pool.with_connection do |ar_conn|
    pg_conn = ar_conn.raw_connection

    unless pg_conn.is_a?(PG::Connection)
      raise "The Active Record database must be PostgreSQL in order to use the PostgreSQL Action Cable storage adapter"
    end

    yield pg_conn
  end
end