Ninchat API connection library.
See: https://ninchat.com/api/v2
The default X-User-Agent string.
Create a Caller object.
- Caller
Call the sessionless API. The returned Promise will be
resolved with an event header array as a parameter to the callback
function, or rejected on connection error. Note that error
events are
delivered via the promise's resolve callback, not via the reject
callback like when using a Session. The notify callback is not used.
See: https://ninchat.com/api/v2#sessionless-http-calling
- Object params Action parameters to send.
- Function [onLog] Message logger.
- String [address] Alternative API endpoint.
- Promise
Create an uninitialized Session object.
- Session
Convert an event's payload part to a string.
- Object data
- String
Caller holds optional configuration for sessionless API calls.
Caller objects may be instantiated only via the newCaller function.
Set an optional message logger. It will be called with a single string argument.
- Function callback
Set an HTTP header. The key must be in canonical (Title-Case) format.
See: https://golang.org/pkg/net/http/#CanonicalHeaderKey
- String key
- String value
Use an alternative API endpoint.
- String address
Call the sessionless API. The returned Promise will be
resolved with an event header array as a parameter to the callback
function, or rejected on connection error. Note that error
events are
delivered via the promise's resolve callback, not via the reject
callback like when using a Session. The notify callback is not used.
See: https://ninchat.com/api/v2#sessionless-http-calling
- Object params Action parameters to send.
- Promise
Session hides the details of API connection management. It needs to be initialized by calling at least the onSessionEvent, onEvent and setParams methods. After that the open method is used to make a connection to the server. Finally, the close method disconnects from the server.
Session objects may be instantiated only via the newSession function.
Set the session creation handler. It will be invoked with a
session_created
or an error
event header as a parameter.
If another session_created
event is received, it means that the
previous session was lost, and a new one was established automatically.
If an error
event is received, it means that a new session can't be
established without intervention. The client code must call setParams()
to supply new credentials, unless it decides to close().
- Function callback
Set the handler for in-session events. It will be invoked with an event header and a payload array parameter.
error
events received via this callback are not fatal.
- Function callback
Set an optional session closure handler. It doesn't take any parameters.
It will be invoked after a close() call has been fully processed. It
won't be invoked if an error
event is received via onSessionEvent
(unless setParams is called again).
- Function callback
Set an optional connection state change monitor. It will be called with one of the following strings:
connecting
connected
disconnected
- Function callback
Set an optional connection activity monitor. It will be called with a timestamp (in milliseconds) indicating the latest time when data was received on the connection.
- Function callback
Set an optional message logger. It will be called with a single string argument.
- Function callback
Set create_session
action parameters. If open() has already been
called, this takes effect when a session is lost.
- Object params Initial action parameters.
Set an HTTP header. The key must be in canonical (Title-Case) format.
See: https://golang.org/pkg/net/http/#CanonicalHeaderKey
- String key
- String value
Use an alternative API endpoint.
- String address
Create a session on the server.
Close the session on the server.
Reconnect now, if reconnection process is currently waiting to retry. This gives a hint that network conditions have changed. It is safe to call this at any time.
Send an action.
An action_id
sequence number is generated automatically; it must not
be specified explicitly. To send an action without an action_id
parameter, specify it as null (the only valid value).
If an action_id
is generated (the default), a Promise is
returned. It may be used to wait for a reply from the server; the
promise will be resolved with an event header and a payload array
parameter. If the Session object is closed before a reply is received,
the promise will be rejected without a parameter.
With specific actions that cause multiple reply events, the notify callback will be called for each event until the final event which resolves the promise.
- Object params Action parameters to send.
- Array [payload] Consists of (already encoded) data frames.
- Promise|Null
Promise objects may not be instantiated directly.
Add callback to be called when the promise is resolved, rejected and/or updated.
- Function [onFulfilled]
- Function [onRejected]
- Function [onNotified]
- Promise