Class: Rage::SSE::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/sse/stream.rb

Overview

The class representing an unbounded Server-Sent Events stream. It allows for broadcasting messages to all connected clients subscribed to the stream.

Create a stream:

render sse: Rage::SSE.stream([current_user, "notifications"])

Broadcast a message to all connections subscribed to the stream:

Rage::SSE.broadcast([current_user, "notifications"], "You have a new notification!")

Close the stream:

Rage::SSE.close_stream([current_user, "notifications"])

Messages to known streams are buffered until a connection is fully established:

# Create a stream first
stream = Rage::SSE.stream([current_user, "notifications"])

# No connection yet, but the message is buffered
Rage::SSE.broadcast([current_user, "notifications"], "You have a new notification!")

# Establish a connection, which will claim the buffered message
render sse: stream

Instance Method Summary collapse

Constructor Details

#initialize(streamable:) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • streamable (#id, String, Symbol, Numeric, Array)

    an object that will be used to generate the stream name



72
73
74
75
76
77
# File 'lib/rage/sse/stream.rb', line 72

def initialize(streamable:)
  @name = Rage::Internal.stream_name_for(streamable)
  @owner = Fiber.current

  self.class.__message_buffer[@name][@owner] ||= DEFAULT_BUFFER
end