class ActionController::Live::SSE

by Chrome, Firefox, Opera, and Safari.
Note: SSEs are not currently supported by IE. However, they are supported
end
end
sse.close
ensure
sse.write({ name: ‘John’}, id: 10, event: “other-event”, retry: 500)
sse.write({ name: ‘John’}, id: 10, event: “other-event”)
sse.write({ name: ‘John’}, id: 10)
sse.write({ name: ‘John’})
sse = SSE.new(response.stream, retry: 300, event: “event-name”)<br>response.headers = ‘text/event-stream’
def index
include ActionController::Live
class MyController < ActionController::Base
Example Usage:
SSEs sent across the stream will use those options unless overridden.
After setting an option in the constructor of the SSE object, all future
the server will receive a Last-Event-ID header with value equal to id.
3) Id. If the connection dies while sending an SSE to the browser, then
to send the event.
2) Retry. The reconnection time in milliseconds used when attempting
the browser.
1) Event. If specified, an event with this name will be dispatched on
options you have configured. You may choose to set the following options:
Writing an object will convert it into standard SSE format with whatever
to either write a JSON string or an object which can be converted to JSON.
to an IO stream. The class is initialized with a stream and can be used
This class provides the ability to write an SSE (Server Sent Event)
= Action Controller Live Server Sent Events

def close

def close
  @stream.close
end

def initialize(stream, options = {})

def initialize(stream, options = {})
  @stream = stream
  @options = options
end

def perform_write(json, options)

def perform_write(json, options)
  current_options = @options.merge(options).stringify_keys
  PERMITTED_OPTIONS.each do |option_name|
    if (option_value = current_options[option_name])
      @stream.write "#{option_name}: #{option_value}\n"
    end
  end
  message = json.gsub("\n", "\ndata: ")
  @stream.write "data: #{message}\n\n"
end

def write(object, options = {})

def write(object, options = {})
  case object
  when String
    perform_write(object, options)
  else
    perform_write(ActiveSupport::JSON.encode(object), options)
  end
end