# API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [Client Functions](#client-functions)
    -   [createClient](#createclient)
    -   [creationResultCallback](#creationresultcallback)
    -   [defaultCreateClientOptions](#defaultcreateclientoptions)
    -   [client](#client)
    -   [send](#send)
    -   [sendCallback](#sendcallback)
    -   [procedureCall](#procedurecall)
    -   [on](#on)
    -   [connectToStreamServer](#connecttostreamserver)
    -   [stream](#stream)
    -   [addStream](#addstream)
    -   [addStreamCallback](#addstreamcallback)
    -   [removeStream](#removestream)
    -   [removeStreamCallback](#removestreamcallback)
    -   [close](#close)
-   [Encoders](#encoders)
    -   [encodeDouble](#encodedouble)
    -   [encodeFloat](#encodefloat)
    -   [encodeSInt32](#encodesint32)
    -   [encodeSInt64](#encodesint64)
    -   [encodeUInt32](#encodeuint32)
    -   [encodeUInt64](#encodeuint64)
    -   [encodeBool](#encodebool)
    -   [encodeString](#encodestring)
    -   [encodeEnum](#encodeenum)
    -   [encodeValueBasedOnEnum](#encodevaluebasedonenum)
-   [Decoders](#decoders)
    -   [decodeDouble](#decodedouble)
    -   [decodeFloat](#decodefloat)
    -   [decodeSInt32](#decodesint32)
    -   [decodeSInt64](#decodesint64)
    -   [decodeUInt32](#decodeuint32)
    -   [decodeUInt64](#decodeuint64)
    -   [decodeBool](#decodebool)
    -   [decodeString](#decodestring)
    -   [decodeEnum](#decodeenum)
    -   [decodeBufferToEnumValue](#decodebuffertoenumvalue)

## Client Functions

The core of the library for interacting with the server


### createClient

Async function that creates a new `krpc-node` client

**Parameters**

-   `options` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** The options used to create the client, defaults to [defaultCreateClientOptions](#defaultcreateclientoptions)
    -   `options.rpc` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** The options used to create the web socket client for primary rpc calls to the server
        -   `options.rpc.protocol` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The protocol to use to connect to the server. `ws` or `wss`. (optional, default `ws`)
        -   `options.rpc.host` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The host address of the server. (optional, default `"127.0.0.1"`)
        -   `options.rpc.port` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number))** The port number on which to connect to the server. (optional, default `"50000"`)
        -   `options.rpc.wsProtocols` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** WebSocket protocols to pass to the [ws](https://www.npmjs.com/package/ws) library.
        -   `options.rpc.wsOptions` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Additional connection options  to pass to the [ws](https://www.npmjs.com/package/ws) library.
    -   `options.stream` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** The options used to create the web socket client for stream rpc calls to the server
        -   `options.stream.protocol` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The protocol to use to connect to the server. `ws` or `wss`. (optional, default `ws`)
        -   `options.stream.host` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The host address of the server. (optional, default `"127.0.0.1"`)
        -   `options.stream.port` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number))** The port number on which to connect to the server. (optional, default `"50000"`)
        -   `options.stream.wsProtocols` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** WebSocket protocols to pass to the [ws](https://www.npmjs.com/package/ws) library.
        -   `options.stream.wsOptions` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Additional connection options  to pass to the [ws](https://www.npmjs.com/package/ws) library.
-   `callback` **[creationResultCallback](#creationresultcallback)?** The function called once the client has been created.

**Examples**

```javascript
const createClient = require('krpc-node');

      createClient(null, clientCreated);

      function clientCreated(err, client) {
          if (err) {
              throw err;
          }
          console.log('Connection Opened');
          client.send(client.services.krpc.getClients(), getClientsCompleted);
      }

      function getClientsCompleted(err, response) {
          if (err) {
              throw err;
          }
          expect(response.error).to.not.be.ok();
          expect(response.results.length).to.equal(1);
          let result = response.results[0];
          expect(result.error).to.not.be.ok();
          result.value.items.forEach(function(item) {
              expect(item).to.be.ok();
              console.log(item);
          });
      }
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[client](#client)>** 

### creationResultCallback

The callback that is called after attempting to create a new client

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

**Parameters**

-   `error` **(null | [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error))** Lets the caller know if there was an error creating the client
-   `client` **[client](#client)** The created client, ready to use

### defaultCreateClientOptions

Default options used to create a client. Gets merged in with the options you provide

**Examples**

```javascript
const defaultCreateClientOptions = {
     rpc: {
         protocol: 'ws',
         host: '127.0.0.1',
         port: '50000',
         wsProtocols: null,
         wsOptions: null
     },
     stream: {
         protocol: 'ws',
         host: '127.0.0.1',
         port: '50001',
         wsProtocols: null,
         wsOptions: null
     }
 };
```

### client

**Parameters**

-   `callbackStack` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)>** An ordered array of callback functions to call when responses are received.
-   `decodeStack` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)>** An ordered array of decode functions to call when responses are received.
-   `rpc` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Contains items related to communicating directly with the server.
    -   `rpc.socket` **[WebSocket](https://developer.mozilla.org/docs/WebSockets)** The underlying websocket instance used for primary communications with the server.
    -   `rpc.emitter` **EventEmitter** The event emitter for primary rpc connection to the server
    -   `rpc.on` **[on](#on)** Registers for one of the events for messages from the server [open, message, error, close].
-   `send` **[send](#send)** Sends a one or more service call(s) to the server see [send](#send). The documentation for available service calls can be found in the services section of the[main README.md](https://github.com/eXigentCoder/krpc-node/blob/master/README.md)
-   `services` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The collection of services that can be called.
    -   `services.krpc` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Main kRPC service, used by clients to interact with basic server functionality.[See the KRPC service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/krpc.md)
    -   `services.spaceCenter` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Provides functionality to interact with Kerbal Space Program. This includes controlling the active vessel, managing its resources, planning maneuver nodes and auto-piloting.[See the SpaceCenter service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/space-center.md)
    -   `services.drawing` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Provides functionality for drawing objects in the flight scene. For drawing and interacting with the user interface.[See the Drawing service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/drawing.md)
    -   `services.ui` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Provides functionality for drawing and interacting with in-game user interface elements. For drawing 3D objects in the flight scene.[See the UI service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/ui.md)
    -   `services.infernalRobotics` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** This service provides functionality to interact with the[Infernal Robotics](http://forum.kerbalspaceprogram.com/index.php?/topic/104535-105-magic-smoke-industries-infernal-robotics-0214) mod.[See the InfernalRobotics service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/infernal-robotics.md)
    -   `services.kerbalAlarmClock` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** This service provides functionality to interact with the[Kerbal Alarm Clock](http://forum.kerbalspaceprogram.com/index.php?/topic/22809-10x-kerbal-alarm-clock-v3500-dec-3/) mod.[See the KerbalAlarmClock service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/kerbal-alarm-clock.md)
    -   `services.remoteTech` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** This service provides functionality to interact with[RemoteTech](href="http://forum.kerbalspaceprogram.com/index.php?/topic/75245-11-remotetech-v1610-2016-04-12/) mod.[See the RemoteTech service.](https://github.com/eXigentCoder/krpc-node/blob/master/documentation/remote-tech.md)
-   `encoders` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The raw [encoders](#encoders) that can be used to manually encode values.
-   `decoders` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The raw [decoders](#decoders) that can be used to manually decode values.
-   `streams` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Will store the streams with the string representation of their unique id as the key. Values are of type [stream](#stream)
-   `Establishes` **[connectToStreamServer](#connecttostreamserver)** a separate connection to the stream server.
-   `addStream` **[addStream](#addstream)** Adds a single call to the stream communication. Make sure you call connectToStreamServer fist.
-   `removeStream` **[removeStream](#removestream)** Removes a single call from the stream communication. Make sure you call connectToStreamServer and of course have called addStream fist.
-   `stream` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Contains items related to communicating with the stream server.
    -   `stream.socket` **[WebSocket](https://developer.mozilla.org/docs/WebSockets)** The underlying websocket instance used to communicate with the server for stream information.
    -   `stream.emitter` **EventEmitter** The emitter that handles events for stream information.
    -   `stream.on` **[on](#on)** Registers for one of the events for messages from the server sent via streams [open, message, error, close].
-   `close` **[close](#close)** Disconnects the client (RPC & Stream) from the server.

### send

Async function which sends one or more request(s) to the server

**Parameters**

-   `procedureCall` **([procedureCall](#procedurecall) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[procedureCall](#procedurecall)>)** A list of rpc procedureCall to make on the server
-   `sendCallback` **[sendCallback](#sendcallback)?** The optional callback to execute when the request is sent.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[response](https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5)>** A promise which will resolve to the response from the server.

### sendCallback

This callback that is called after attempting to send calls to the server

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

**Parameters**

-   `error` **(null | [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error))** Lets the caller know if there was an error sending the calls to the server
-   `response` **[response](https://developer.mozilla.org/docs/Web/Guide/HTML/HTML5)** The server response.

### procedureCall

An object which represents a remote procedure call to execute on the server

**Parameters**

-   `decode` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** A function used to decode the response when it is returned by the server
-   `call` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The actual call and any arguments to send to the server to execute.

### on

Function that allows you to register for one of the events relating to the websocket [open, message, error, close].

**Parameters**

-   `eventName`  The name of the event to register for
-   `fn`  the function to execute when the event occurs

### connectToStreamServer

Connects to the stream server in order to stream continuous updates

**Parameters**

-   `clientId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The clientId returned via krpc.getClientId
-   `connectToStreamServerCallback`  [callback] The callback to execute when done connecting

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;void>** A promise to resolve when done connecting

### stream

An object representing a proceedure call that is being streamed from the server

**Parameters**

-   `propertyPath` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A unique name to represent the call
-   `decode` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The function used to decode responses for the server for this call
-   `id` **Long** A Long.js representation of the 64bit integer used to uniquely identify this stream on the server
-   `value` **any?** The last known value of the call

### addStream

Adds an call to the continuous update stream.

**Parameters**

-   `procedureCall` **[procedureCall](#procedurecall)** The call to add to the stream
-   `propertyPath` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A unique name to represent the call
-   `callback` **[addStreamCallback](#addstreamcallback)?** the callback function to execute when the operation has ended

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[stream](#stream)>** The stream that was added

### addStreamCallback

This callback that is called after attempting to add a call to the stream

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

**Parameters**

-   `error` **(null | [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error))** Lets the caller know if there was an error adding the call to the stream
-   `The` **[stream](#stream)** stream that was added

### removeStream

Removes a call from the continuous update stream.

**Parameters**

-   `propertyPath`  A unique name that represents the existing call that is being streamed
-   `callback` **[removeStreamCallback](#removestreamcallback)?** The callback function to execute when the operation has ended

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;void>** If successful with resolve to nothing

### removeStreamCallback

This callback that is called after attempting to remove a call from the stream

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

**Parameters**

-   `error` **(null | [Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error))** Lets the caller know if there was an error sending the calls to the server

### close

Closes both the stream and rpc socket connection to the server. Should be called to free up resources and end the event loop.

**Parameters**

-   `callback` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** The callback to execute after closing.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;void>** 

## Encoders

Functions to help with raw encoding of values to send to the server


### encodeDouble

Takes in a value and encodes it as a `double` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeFloat

Takes in a value and encodes it as a `float` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeSInt32

Takes in a value and encodes it as a `sInt32` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeSInt64

Takes in a value and encodes it as a `sInt64` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeUInt32

Takes in a value and encodes it as a `uInt32` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeUInt64

Takes in a value and encodes it as a `uInt64` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeBool

Takes in a value and encodes it as a `bool` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeString

Takes in a value and encodes it as a `string` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.

Returns **(ByteBuffer | void)** 

### encodeEnum

Returns a function that can be used to encode a string as the specific enum value.

**Parameters**

-   `enumDefinition` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The key-value enum object. Keys should be numbers, values should be strings.

Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The [function](#encodeValueBasedOnEnum) that will do the encoding.

### encodeValueBasedOnEnum

Takes in a string value and using the provided enum definition encodes it as a `sInt` stored in a [ByteBuffer]<https://www.npmjs.com/package/bytebuffer> object for use with the protobufjs library.

**Parameters**

-   `value`  The value to encode.


-   Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** If the provided value was not found in the enum definition

Returns **(ByteBuffer | void)** 

## Decoders

Functions to help with raw decoding of values to send to the server


### decodeDouble

Takes in a node.js buffer object representing a `double` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | any)** 

### decodeFloat

Takes in a node.js buffer object representing a `float` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **([number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | any)** 

### decodeSInt32

Takes in a node.js buffer object representing a `sInt32` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 

### decodeSInt64

Takes in a node.js buffer object representing a `sInt64` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **(!Long | !{value: Long, length: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)} | !Long | {value: !Long, length: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)})** 

### decodeUInt32

Takes in a node.js buffer object representing a `uInt32` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **({value, length} | [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) | !{value: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), length: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)})** 

### decodeUInt64

Takes in a node.js buffer object representing a `uInt64` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **any** 

### decodeBool

Takes in a node.js buffer object representing a `bool` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 

### decodeString

Takes in a node.js buffer object representing a `string` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object

Returns **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | !{string: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), length: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)} | {string, length})** 

### decodeEnum

Returns a function that can be used to decode a node.js buffer into an entry from the provided enum definition.

**Parameters**

-   `enumDefinition` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The key-value enum object. Keys should be numbers, values should be strings.

Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The [function](#decodeBufferToEnumValue) that will do the decoding.

### decodeBufferToEnumValue

Takes in a node.js buffer object representing a `double` and decodes it.

**Parameters**

-   `buffer` **ByteBuffer** The buffer object
