Class: EventMachine::EventSource

Inherits:
Object
  • Object
show all
Defined in:
lib/em-eventsource.rb

Overview

EventSource dev.w3.org/html5/eventsource/

Constant Summary

CONNECTING =

Ready state The connection has not yet been established, or it was closed and the user agent is reconnecting.

0
OPEN =

The user agent has an open connection and is dispatching events as it receives them.

1
CLOSED =

The connection is not open, and the user agent is not trying to reconnect. Either there was a fatal error or the close() method was invoked.

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, query = {}, headers = {}) ⇒ EventSource

Create a new stream

url - the url as string query - the query string as hash headers - the headers for the request as hash



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/em-eventsource.rb', line 37

def initialize(url, query={}, headers={})
  @url = url
  @query = query
  @headers = headers
  @ready_state = CLOSED

  @last_event_id = nil
  @retry = 3 # seconds
  @inactivity_timeout = 60 # seconds

  @opens = []
  @errors = []
  @messages = []
  @on = {}
  @middlewares = []
end

Instance Attribute Details

#inactivity_timeoutObject

Get the inactivity timeout



21
22
23
# File 'lib/em-eventsource.rb', line 21

def inactivity_timeout
  @inactivity_timeout
end

#last_event_idObject (readonly)

Get value of last event id



19
20
21
# File 'lib/em-eventsource.rb', line 19

def last_event_id
  @last_event_id
end

#ready_stateObject (readonly)

Get ready state



13
14
15
# File 'lib/em-eventsource.rb', line 13

def ready_state
  @ready_state
end

#retryObject

Get current retry value (in seconds)



15
16
17
# File 'lib/em-eventsource.rb', line 15

def retry
  @retry
end

#urlObject (readonly)

Get API url



11
12
13
# File 'lib/em-eventsource.rb', line 11

def url
  @url
end

Instance Method Details

#closeObject

Cancel subscription

Returns nothing



105
106
107
108
# File 'lib/em-eventsource.rb', line 105

def close
  @ready_state = CLOSED
  @conn.close('requested') if @conn
end

#error(&block) ⇒ Object

Add error event handler

Returns nothing



81
82
83
# File 'lib/em-eventsource.rb', line 81

def error(&block)
  @errors << block
end

#message(&block) ⇒ Object

Add message event handler

Returns nothing



74
75
76
# File 'lib/em-eventsource.rb', line 74

def message(&block)
  @messages << block
end

#on(name, &block) ⇒ Object

Add a specific event handler

name - name of event

Returns nothing



66
67
68
69
# File 'lib/em-eventsource.rb', line 66

def on(name, &block)
  @on[name] ||= []
  @on[name] << block
end

#open(&block) ⇒ Object

Add open event handler

Returns nothing



57
58
59
# File 'lib/em-eventsource.rb', line 57

def open(&block)
  @opens << block
end

#startObject

Start subscription

Returns nothing



97
98
99
100
# File 'lib/em-eventsource.rb', line 97

def start
  @ready_state = CONNECTING
  listen
end

#use(*args, &block) ⇒ Object

Add a middleware

*args - the middleware class

Returns nothing



90
91
92
# File 'lib/em-eventsource.rb', line 90

def use(*args, &block)
  @middlewares << (args << block)
end