A websocket based communication protocol.
npm install especial
const especial = require('especial')
const app = especial()
app.handle('utils.ping', (data, send, next) => {
send('pong')
})
const server = app.listen(4000, () => {
console.log(`Listening on port 4000`)
})
const EspecialClient = require('especial/client')
const client = new EspecialClient('ws://localhost:4000')
const { data, message, status } = await client.send('utils.ping')
console.log(message) // "pong"
Especial communicates across websockets using JSON encoded payloads. Clients emit a payload with a unique id and the server is expected to respond with a single message with the same id.
Requests are structured like this:
{
"_rid": "jki7uo9XsOEJkel3PrF_T",
"route": "utils.ping",
"data": {}
}
The fields are as follows:
_rid
: A unique identifier for the requestroute
: A string indicating the function to executedata
: An object containing arbitrary data for the function
Responses are structured like this:
{
"_rid": "jki7uo9XsOEJkel3PrF_T",
"route": "utils.ping",
"data": {},
"message": "pong",
"status": 0
}
_rid
: The same _rid emitted in the matching requestroute
: The route requesteddata
: Arbitrary data returned from the functionmessage
: String info about function executionstatus
: Integer representing execution result,0
indicates success
In addition to simple request/response communication servers may send data to clients without a request being made. This can be used to update data or provide new information.
The structure of such a message is the same as a response, with the _rid
being a simple string the client may subscribe to.