Class: JSONBottleServer

JSONBottleServer

JSONBottleServer

new JSONBottleServer()

Example
const JSONBottleServer = require('json-bottle').Server;
const server = new JSONBottleServer();

Extends

Methods


clearPendingRequests( [error])

Clears all pending requests by failing them
Parameters:
Name Type Argument Description
error Error <optional>
Error to reject pending requests with
Inherited From:
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
Inherited From:
Returns:
- New message bound to this interface
Type
JSONBottleMessage

destroyAllSockets()

Destroy all sockets
Inherited From:
Returns:
- Self for chaining
Type
JSONBottleInterface

destroySocket(id)

Destroy socket
Parameters:
Name Type Description
id string Socket id
Inherited From:
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
Inherited From:
Returns:
- Value for the key or undefined
Type
mixed

getSocket(id)

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

getSockets()

Get all sockets as array
Inherited From:
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
Inherited From:
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
Inherited From:
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
Inherited From:
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
Inherited From:
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
Inherited From:
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
Inherited From:
Returns:
- Promise representing the request
Type
Promise

setupSocket(socket)

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

start(port, host)

Start JSONBottleServer instance
Parameters:
Name Type Description
port number Port to listen to
host string Host to bind to
Returns:
- Self instance for chaining
Type
JSONBottleServer

stop(cb)

Stop the instance
Parameters:
Name Type Description
cb function Callback function called once the undelying server is stopped
Returns:
- Self instance for chaining
Type
JSONBottleServer

use(callback)

Use handler for all incoming messages
Parameters:
Name Type Argument Description
callback JSONBottleInterface~incomingMessageHandlerCallback <repeatable>
Message handler function
Inherited From:
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
Inherited From:
Returns:
- Self for chaining
Type
JSONBottleInterface