# connManager

Takes care of remote tcp-ip connections by attempting automatic reconnects and handling timeouts.

Version:
  - 1.0.0
Author:
  - Pedro Miguel P. S. Martins
Source:
  - connectionManager.js, line 7

### Methods

#### (inner) connect(connectOpts) → {Promise}

Attempts to connect to the tcp-ip server provided with the given options.

It will not resolve until a connection is made. Once a connection is made it fires `onOpen`, and every time it retries to establish a connection it fires `onRetry()`.

Once a connection is established, it starts the heartbeat to periodically check the health of the target. If the connection dies or times out, it fires `onClose` and automatic reconnection is attempted, called `onRetry` for every failed attempt.

Source:
  - connectionManager.js, line 42
See:
  - net.createConnection()
  - `onOpen`
  - `onRetry`
  - `onClose`

##### Parameters:

Name | Type | Description
--- | --- | ---
`connectOpts` | `Object` | An object with connect options. This is the object that will be passed to `connectFn` to attempt connect. The default `connectFn` makes a socket connection, so the connections object passed to the default should be the one used is `net.createConnection()`.

##### Throws:

If there is no connection options. There must always be a `connectOpts` parameter.

Type
  `Error`

##### Returns:

Type:
  `Promise`

#### (inner) disconnect()

Kills the connection to the target, stops the heartbeat and fires `onClose`

Source:
  - connectionManager.js, line 121
See:
  - `onClose`

#### (inner) isConnected() → {Boolean}

Returns `true` if the connection is up, `false` otherwise. If a reconnection is taking place, it will still return `false`.

Source:
  - connectionManager.js, line 135

##### Returns:

Type:
  `Boolean`

`true` if the connection is up, `false` otherwise.

#### (inner) onClose(newFn)

Runs the given function every time the onClose event is triggered, passing it the argument `false`.

Source:
  - connectionManager.js, line 174

##### Parameters:

Name | Type | Description
--- | --- | ---
`newFn` | `function` | The function to run when the onClose event is fired.

##### Throws:

If `newFn` is not a function.

Type
  `TypeError`

#### (inner) onOpen(newFn)

Runs the given function every time the onOpen event is triggered, passing it the argument code>true.

Source:
  - connectionManager.js, line 191

##### Parameters:

Name | Type | Description
--- | --- | ---
`newFn` | `function` | The function to run when the onOpen event is fired.

##### Throws:

If `newFn` is not a function.

Type
  `TypeError`

#### (inner) onRead(newFn)

Runs the given function every time the onRead event is triggered, passing it the received data as an argument.

Source:
  - connectionManager.js, line 208

##### Parameters:

Name | Type | Description
--- | --- | ---
`newFn` | `function` | The function to run when the onRead event is fired.

##### Throws:

If `newFn` is not a function.

Type
  `TypeError`

#### (inner) onRetry(newFn)

Runs the given function every time the onRetry event is triggered, passing it the error that caused the retry, as well as a count of the number of retries.

Source:
  - connectionManager.js, line 225

##### Parameters:

Name | Type | Description
--- | --- | ---
`newFn` | `function` | The function to run when the onRetry event is fired.

##### Throws:

If `newFn` is not a function.

Type
  `TypeError`

#### (inner) send(message)

Sends a message to the target.

Source:
  - connectionManager.js, line 157
See:
  - `isConnected`

##### Parameters:

Name | Type | Description
--- | --- | ---
`message` | `Object` | The message object we want to send.

##### Throws:

If the connection is down. You can check this via `isConnected`.

Type
  `Error`

#### (inner) setConnectFn(newFn)

Used when you need to custom connect function because the target has a specific handshake or protocol. The function passed must return the socket used for the connection.

Source:
  - connectionManager.js, line 242

##### Parameters:

Name | Type | Description
--- | --- | ---
`newFn` | `function` | The function to be used when `connect` is called and every time automatic reconnection takes place.

##### Throws:

If `newFn` is not a function.

Type
  `TypeError`