SSE-IO Client-JS
JavaScript Client For SSE-IO. You can use it with Node.js or as a browser polyfill for browsers that don't have native EventSource support.
How to use
You can serve the file sse.io-client.js
found in the dist
folder or include it via CDN
Import using ES6
; const client = sseio;
API
SSEIO
you should always create a client first
client(url, events, options)
url
(String, Required) url path for SSE connection.events
(Array[String], Required) theEventSource
created by client will addEventListener to the events. Also, it will be add to query params to the SSE http request.options
(Object, Optional)reconnect
(Boolean) (default totrue
) client will auto reconnect when can't connect to server, connections closed by server or receiving 5xx http status.backoffOptions
(Object) implements from backo. Client will delay reconnect when receiving 5xx http status or can't connect to server.
- Returns
Client
Client
client.start(options)
options
(Object, Optional)headers
(Object) implements from eventsoureproxy
(String) implements from eventsourehttps
(Object) implements from eventsourewithCredentials
(Boolean) when you send CORS request, you can set it totrue
to enable sending cookie.forceXhr
(Boolean) useXMLHttpRequest
whenFetch
is not support by some browers.queryParams
(Object) your custom query parameters
Creates a new EventSource, establishing the SSE connection, and register listeners for the events
.
client.stop()
Close the EventSource, as well as closing the SSE connection.
client.restart()
Equals to client.stop() && client.start()
, using the latest options.
client.addQueryParams(params)
params
(Object) your custom query parameters
Add query parameters for SSE request. It will be passed to the server when the SSE connection is established next time.
client.isConnected()
- Returns (Boolean)
Whether or not The SSE connection is connected to the server.
client.addEvent(event, queryParams)
event
(String, Required)params
(Object, Optional)
Add an event to events
. You can add query params too. Then the client will restart to make it take effect.
client.removeEvent(event)
event
(String, Required)
Remove an event from events
. Then the client will restart to make it take effect.
client.on(eventName, callback)
Register a handler for the event
Event: 'message'
callback
(Function) an Objectdata
will be passed to the callback functiondata.event
(String)data.message
(String)
Handle received message from server for the registered events.
client
Event: 'error'
callback
(Function) anError
will be passed to the callback functionerror.message
(String)error.status
(Number) (default to -1) the http status received from servererror.reason
(String) possible values: 'can't connect to server', 'http error'
Handle error message. Including the http error as well as the EventSource
error or close message.
client
Event: 'connected'
Fired upon a connection.
Event: 'disconnect'
Fired upon a disconnection.