Class: JSONBottleInterface

JSONBottleInterface

JSONBottleInterface

new JSONBottleInterface( [config])

Constructor
Parameters:
Name Type Argument Description
config JSONBottleInterface~config <optional>
Interface configuration
Example
const JSONBottleInterface = require('json-bottle').Interface;
const iface = new JSONBottleInterface();

Methods


clearPendingRequests( [error])

Clears all pending requests by failing them
Parameters:
Name Type Argument Description
error Error <optional>
Error to reject pending requests with
Returns:
- Self for chaining
Type
JSONBottleInterface

createMessage(path, body)

Create new message bound to this interface
Parameters:
Name Type Description
path string Message path
body Object Message body
Returns:
- New message bound to this interface
Type
JSONBottleMessage

destroyAllSockets()

Destroy all sockets
Returns:
- Self for chaining
Type
JSONBottleInterface

destroySocket(id)

Destroy socket
Parameters:
Name Type Description
id string Socket id
Returns:
- Socket with the passed id or undefined
Type
external:Socket | undefined

getConfig(key)

Get config key
Parameters:
Name Type Description
key string Key to get the config for
Returns:
- Value for the key or undefined
Type
mixed

getSocket(id)

Get socket by ID
Parameters:
Name Type Description
id string Socket id
Returns:
- Socket with the passed id or undefined
Type
external:Socket | undefined

getSockets()

Get all sockets as array
Returns:
- All sockets
Type
Array.<external:Socket>

handleIncomingData(data [, socket])

Create message from input data. Mainly intended for internal use, but can be used from outside to inject message into the handling cycle.
Parameters:
Name Type Argument Description
data string | JSONBottleMessage~data Either raw JSON string or a message data object (if object, id can be passed as well)
socket external:Socket <optional>
If present, represents the socket this message came from, otherwise the message returned will be treated as locally injected
Returns:
- New message object
Type
JSONBottleMessage

handleIncomingMessage(message)

Process message through the next registered handler
Parameters:
Name Type Description
message JSONBottleMessage Incoming message to process
Returns:
- Self for chaining
Type
JSONBottleInterface

request(path, body [, socketId] [, timeout])

Issue request
Parameters:
Name Type Argument Description
path JSONBottleMessage~data.path Send on specified path
body JSONBottleMessage~data.body Body to send
socketId string <optional>
Id of the socket to issue the request to (request will be sent to the first found socket if not provided)
timeout number <optional>
Response timeout in milliseconds, 10000 by default
Returns:
- Promise representing the request
Type
Promise

send(path, body [, socketId])

Send body on path to all sockets
Parameters:
Name Type Argument Description
path JSONBottleMessage~data.path Send on specified path
body JSONBottleMessage~data.body Body to send
socketId string | Array.<string> <optional>
If provided, sends to socket id(s) specified, otherwise sends to all socket
Returns:
- Freshly sent message
Type
JSONBottleMessage

sendMessage(message, sockets [, callback])

Send message
Parameters:
Name Type Argument Description
message JSONBottleMessage Message to send
sockets Array.<external:Socket> Array of sockets to send the message to
callback JSONBottleInterface~outgoingMessageCallback <optional>
Outgoing message callback
Returns:
- Self for chaining
Type
JSONBottleInterface

sendMessageRequest(message, socket, timeout)

Send message as a request (await response)
Parameters:
Name Type Description
message JSONBottleMessage Message that is issuing a request
socket external:Socket Socket to send the request to
timeout number Response timeout in milliseconds
Returns:
- Promise representing the request
Type
Promise

setupSocket(socket)

Setup socket
Parameters:
Name Type Description
socket external:Socket Socket to setup
Returns:
- Passed socket
Type
external:Socket

use(callback)

Use handler for all incoming messages
Parameters:
Name Type Argument Description
callback JSONBottleInterface~incomingMessageHandlerCallback <repeatable>
Message handler function
Returns:
- Self for chaining
Type
JSONBottleInterface

useOnce(callback)

Use handler for any incoming message only once
Parameters:
Name Type Argument Description
callback JSONBottleInterface~incomingMessageHandlerCallback <repeatable>
Message handler function
Returns:
- Self for chaining
Type
JSONBottleInterface

Type Definitions


config

Interface configuration object
Type:
  • object
Properties:
Name Type Argument Default Description
timeout number <optional>
10000 Default timeout to use for the requests

incomingMessageHandler

Incoming message handler object
Type:
  • object
Properties:
Name Type Description
test RegExp | null Path tester
callback JSONBottleInterface~incomingMessageHandlerCallback Handler callback

incomingMessageHandlerCallback(message)

Incoming message handler callback
Parameters:
Name Type Description
message JSONBottleMessage Incoming message being handled

outgoingMessageCallback(error)

Outgoing message callback
Parameters:
Name Type Description
error Error | null The last error detected while sending to all destination sockets. No error will not abort the sending process.