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.



8
9
10
11
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 8

def initialize(*)
  super
  @listener = nil
end

Instance Method Details

#broadcast(channel, payload) ⇒ Object



13
14
15
16
17
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 13

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

#shutdownObject



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

def shutdown
  listener.shutdown
end

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



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

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

#unsubscribe(channel, callback) ⇒ Object



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

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

#with_connection(&block) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/action_cable/subscription_adapter/postgresql.rb', line 31

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 'ActiveRecord database must be Postgres in order to use the Postgres ActionCable storage adapter'
    end

    yield pg_conn
  end
end