Class: Pakyow::Realtime::Server::Adapters::Memory Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/realtime/server/adapters/memory.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages websocket channels in memory.

Great for development, not for use in production!

Constant Summary collapse

SERIALIZABLE_IVARS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i(
  @socket_ids_by_channel
  @channels_by_socket_id
  @socket_instances_by_socket_id
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(server, _config) ⇒ Memory

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Memory.



17
18
19
20
21
22
23
24
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 17

def initialize(server, _config)
  @server = server

  @socket_ids_by_channel = Concurrent::Hash.new
  @channels_by_socket_id = Concurrent::Hash.new
  @expirations_for_socket_id = Concurrent::Hash.new
  @socket_instances_by_socket_id = Concurrent::Hash.new
end

Instance Method Details

#connectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 26

def connect
  # intentionally empty
end

#current!(socket_id, socket_instance_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 84

def current!(socket_id, socket_instance_id)
  @socket_instances_by_socket_id[socket_id] = socket_instance_id
end

#current?(socket_id, socket_instance_id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


88
89
90
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 88

def current?(socket_id, socket_instance_id)
  @socket_instances_by_socket_id[socket_id] == socket_instance_id
end

#disconnectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 30

def disconnect
  # intentionally empty
end

#expire(socket_id, seconds) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 62

def expire(socket_id, seconds)
  task = Concurrent::ScheduledTask.execute(seconds) {
    channels_for_socket_id(socket_id).each do |channel|
      @channels_by_socket_id.delete(socket_id)
      @socket_ids_by_channel[channel].delete(socket_id)
      @socket_instances_by_socket_id.delete(socket_id)
    end
  }

  @expirations_for_socket_id[socket_id] ||= []
  @expirations_for_socket_id[socket_id] << task
end

#expiring?(socket_id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


80
81
82
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 80

def expiring?(socket_id)
  @expirations_for_socket_id[socket_id]&.any?
end

#persist(socket_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



75
76
77
78
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 75

def persist(socket_id)
  (@expirations_for_socket_id[socket_id] || []).each(&:cancel)
  @expirations_for_socket_id.delete(socket_id)
end

#serializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 98

def serialize
  SERIALIZABLE_IVARS.each_with_object({}) do |ivar, hash|
    hash[ivar] = instance_variable_get(ivar)
  end
end

#socket_subscribe(socket_id, *channels) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
37
38
39
40
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 34

def socket_subscribe(socket_id, *channels)
  channels.each do |channel|
    channel = channel.to_s.to_sym
    (@socket_ids_by_channel[channel] ||= Concurrent::Array.new) << socket_id
    (@channels_by_socket_id[socket_id] ||= Concurrent::Array.new) << channel
  end
end

#socket_unsubscribe(*channels) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 42

def socket_unsubscribe(*channels)
  channels.each do |channel|
    channel = Regexp.new(channel.to_s)

    @socket_ids_by_channel.select { |key|
      key.to_s.match?(channel)
    }.each do |key, socket_ids|
      @socket_ids_by_channel.delete(key)

      socket_ids.each do |socket_id|
        @channels_by_socket_id[socket_id]&.delete(key)
      end
    end
  end
end

#subscription_broadcast(channel, message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/pakyow/realtime/server/adapters/memory.rb', line 58

def subscription_broadcast(channel, message)
  @server.transmit_message_to_connection_ids(message, socket_ids_for_channel(channel))
end