Class: ActionCable::SubscriptionAdapter::PostgreSQL

Inherits:
Base
  • Object
show all
Includes:
ChannelPrefix
Defined in:
actioncable/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

Methods inherited from Base

#identifier

Constructor Details

#initializePostgreSQL

Returns a new instance of PostgreSQL.


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

def initialize(*)
  super
  @listener = nil
end

Instance Method Details

#broadcast(channel, payload) ⇒ Object


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

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

#shutdownObject


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

def shutdown
  listener.shutdown
end

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


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

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

#unsubscribe(channel, callback) ⇒ Object


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

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

#with_broadcast_connection(&block) ⇒ Object

:nodoc:


50
51
52
53
54
55
56
# File 'actioncable/lib/action_cable/subscription_adapter/postgresql.rb', line 50

def with_broadcast_connection(&block) # :nodoc:
  ActiveRecord::Base.connection_pool.with_connection do |ar_conn|
    pg_conn = ar_conn.raw_connection
    verify!(pg_conn)
    yield pg_conn
  end
end

#with_subscriptions_connection(&block) ⇒ Object

:nodoc:


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'actioncable/lib/action_cable/subscription_adapter/postgresql.rb', line 35

def with_subscriptions_connection(&block) # :nodoc:
  ar_conn = ActiveRecord::Base.connection_pool.checkout.tap do |conn|
    # Action Cable is taking ownership over this database connection, and
    # will perform the necessary cleanup tasks
    ActiveRecord::Base.connection_pool.remove(conn)
  end
  pg_conn = ar_conn.raw_connection

  verify!(pg_conn)
  pg_conn.exec("SET application_name = #{pg_conn.escape_identifier(identifier)}")
  yield pg_conn
ensure
  ar_conn.disconnect!
end