The entrypoint to the library is the `sylkrtc` object. Several objects (`Connection`, `Account` and `Call`) inherit from Node's `EventEmitter` class, you may want to check [its documentation](https://nodejs.org/api/events.html).
### sylkrtc
The main entrypoint to the library. It exposes the main function to connect to SylkServer and some utility functions for general use.
#### sylkrtc.createConnection(options={})
Creates a `sylkrtc` connection towards a SylkServer instance. The supported options are "server" and optional object "userAgent". Where server should point to the WebSocket endpoint of the WebRTC gateway application. Example: `wss://1.2.3.4:8088/webrtcgateway/ws`.
It returns a `Connection` object.
Example:
let connection = sylkrtc.createConnection({server: 'wss://1.2.3.4:8088/webrtcgateway/ws'});
If the optional userAgent object is given, it should contain:
* `name` : string with the name of the application.
* `attachMediaStream`: function to easily attach a media stream to an element. It reexports [attachmediastream](https://github.com/otalk/attachMediaStream).
* `closeMediaStream`: function to close the given media stream.
* `sanatizeHtml`: function to XSS sanitize html strings
### Connection
Object representing the interaction with SylkServer. Multiple connections can be created with
`sylkrtc.createConnection`, but typically only one is needed. Reconnecting in case the connection is interrupted is
taken care of automatically.
Events emitted:
* **stateChanged**: indicates the WebSocket connection state has changed. Two arguments are provided: `oldState` and
`newState`, the old connection state and the new connection state, respectively. Possible state values are: null,
connecting, connected, ready, disconnected and closed. If the connection is involuntarily interrupted the state will
transition to disconnected and the connection will be retried. Once the closed state is set, as a result of the user
calling Connection.close(), the connection can no longer be used or reconnected.
* **publicKey**: emitted after a publicKey lookup. One argument is provided: the result from the lookup request. This
is an object with the following attributes publicKey and uri.
#### Connection.addAccount(options={}, cb=null)
Configures an `Account` to be used through `sylkrtc`. 2 options are required: *account* (the account ID) and
*password*. An optional *displayName* can be set. The account won't be registered, it will just be created.
Optionally *realm* can be passed, which will be used instead of the domain for the HA1 calculation.
The *password* won't be stored or transmitted as given, the HA1 hash (as used in
[Digest access authentication](https://en.wikipedia.org/wiki/Digest_access_authentication)) is created and used instead.
The `cb` argument is a callback which will be called with an error and the account object
Send a message to (SIP) uri. The message will be send with IMDN enabled. `message` should contain a string, `type` should contain the message content type like
'text/plain', 'text/html', 'image/png'. The function returns an instance of `Message`.
Options can contain a timestamp key with a Date Object. The callback will return with an optional error if the message was sent.
Removes all messages from and to `uri` from the (local) account object and server. If you have other devices online, they will get a `removeConversation` event.
### Call
Object representing a audio/video call. Signalling is done using SIP underneath.
Events emitted:
* **localStreamAdded**: emitted when the local stream is added to the call. A single argument is provided: the stream itself.
* **streamAdded**: emitted when a remote stream is added to the call. A single argument is provided: the stream itself.
* **stateChanged**: indicates the call state has changed. Three arguments are provided: `oldState`, `newState` and
`data`. `oldState` and `newState` indicate the previous and current state respectively, and `data` is a generic
per-state data object. Possible states:
* terminated: the call has ended (the `data` object contains a `reason` attribute)
* accepted: the call has been accepted (either locally or remotely)
* incoming: initial state for incoming calls
* progress: initial state for outgoing calls
* early-media: the call has an session description before it is accepted
* established: call media has been established, in case of early media this happens before accepted
* **dtmfToneSent**: emitted when one of the tones passed to `sendDtmf` is actually sent. An empty tone indicates all tones have
finished playing.
#### Call.answer(options={})
Answer an incoming call. Supported options:
* pcConfig: configuration options for `RTCPeerConnection`. [Reference](http://w3c.github.io/webrtc-pc/#configuration).