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

- (EventSource) initialize(url, query = {}, headers = {})

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

- (Object) inactivity_timeout

Get the inactivity timeout



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

def inactivity_timeout
  @inactivity_timeout
end

- (Object) last_event_id (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

- (Object) ready_state (readonly)

Get ready state



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

def ready_state
  @ready_state
end

- (Object) retry

Get current retry value (in seconds)



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

def retry
  @retry
end

- (Object) url (readonly)

Get API url



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

def url
  @url
end

Instance Method Details

- (Object) close

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

- (Object) error(&block)

Add error event handler

Returns nothing



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

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

- (Object) message(&block)

Add message event handler

Returns nothing



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

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

- (Object) on(name, &block)

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

- (Object) open(&block)

Add open event handler

Returns nothing



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

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

- (Object) start

Start subscription

Returns nothing



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

def start
  @ready_state = CONNECTING
  listen
end

- (Object) use(*args, &block)

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