Class: Supabase::Realtime::Message

Inherits:
Struct
  • Object
show all
Defined in:
lib/supabase/realtime/message.rb

Overview

A Phoenix Channel frame: { event, topic, payload, ref, join_ref }. Used both for outbound pushes and parsed inbound messages.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eventObject

Returns the value of attribute event

Returns:

  • (Object)

    the current value of event



11
12
13
# File 'lib/supabase/realtime/message.rb', line 11

def event
  @event
end

#join_refObject

Returns the value of attribute join_ref

Returns:

  • (Object)

    the current value of join_ref



11
12
13
# File 'lib/supabase/realtime/message.rb', line 11

def join_ref
  @join_ref
end

#payloadObject

Returns the value of attribute payload

Returns:

  • (Object)

    the current value of payload



11
12
13
# File 'lib/supabase/realtime/message.rb', line 11

def payload
  @payload
end

#refObject

Returns the value of attribute ref

Returns:

  • (Object)

    the current value of ref



11
12
13
# File 'lib/supabase/realtime/message.rb', line 11

def ref
  @ref
end

#topicObject

Returns the value of attribute topic

Returns:

  • (Object)

    the current value of topic



11
12
13
# File 'lib/supabase/realtime/message.rb', line 11

def topic
  @topic
end

Class Method Details

.parse(raw) ⇒ Object

Parse a raw JSON frame received on the WebSocket into a Message. Returns nil (and logs a warning) when the frame isn’t well-formed JSON — the caller (read-loop in Client#handle_inbound) treats nil as “skip this frame” so a single garbled byte sequence can’t kill the loop. Closes US-017 / F-C-minor: previously raised ProtocolError and propagated up into the socket adapter, taking down the read thread on first bad frame.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/supabase/realtime/message.rb', line 28

def self.parse(raw)
  json = JSON.parse(raw)
  new(
    event:    json["event"],
    topic:    json["topic"],
    payload:  json["payload"] || {},
    ref:      json["ref"],
    join_ref: json["join_ref"]
  )
rescue JSON::ParserError => e
  msg = "[Supabase::Realtime] Skipping malformed Phoenix frame: #{e.message}"
  if defined?(@logger) && @logger.respond_to?(:warn)
    @logger.warn(msg)
  else
    warn(msg)
  end
  nil
end

Instance Method Details

#to_jsonObject



12
13
14
15
16
17
18
19
20
# File 'lib/supabase/realtime/message.rb', line 12

def to_json(*)
  JSON.generate(
    "event"    => event,
    "topic"    => topic,
    "payload"  => payload,
    "ref"      => ref,
    "join_ref" => join_ref
  )
end