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. Raises ProtocolError if the frame isn’t well-formed JSON.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/supabase/realtime/message.rb', line 24

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
  raise Errors::ProtocolError, "Malformed Phoenix frame: #{e.message}"
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