4.1.3 • Published 12 months ago

restocket v4.1.3

Weekly downloads
5
License
MIT
Repository
github
Last release
12 months ago

Restocket

This library allows you to build API's like in Express, but that also expose a websocket and http(s) endpoint.

Example Usage

Server Side

import { RestocketServer, RestocketRouter } from 'restocket';
const server = new RestocketServer()

// Receive an event when a client connects via websocket
server.onSocketConnected(function (req, res) {
  console.log('Websocket is connected...')
})

// Add a new route
server.get('/hello/:name', function (req, res) {
  res.send({ message: `Hello ${req.params.name}` })
})

// You can also use other Restocket instances
const routes = new RestocketRouter()
routes.get('/goodbye/:name', function (req, res) {
  res.send({ message: `Goodbye ${req.params.name}` })
})

server.use(routes)

// Start the websocket and http instance
await server.start({
  port: 3000
})

console.log('Listening')

Client Side

import { RestocketClient } from 'restocket';

const api = new RestocketClient({
  host: '127.0.0.1:3000'
})

const result = await api.get('/hello/tester')
console.log(result)

How it works

In the above example the endpoints /hello/:name and /goodbye/:name have been created on the websocket and http servers.

You can make a HTTP request to http://127.0.0.1/hello/tester as you would normally in express.

You can make a websocket request to ws://127.0.0.1 with the following data:

// string Method, string Path, optional object Headers, optional any Body
["GET", "/hello/tester"]

Correlation ID

Unlike http requests, websocket queries do not respond one for one. Or at least there is no built in way to related a request with a response.

If you pass a _cid header with your request, the response from the server will also contain the same correlation id in the header.

For example:

  1. Send the following to the websocket
["GET", "/hello/tester", { "_cid": 1 }]
  1. Receive the following from the server
["RESP", { "_cid": 1 }, { "message": "Hello tester" }]

Using RestocketClient

The above is a low level description of using the websocket api directly. However if you use the RestocketClient this is hidden from you in the form of promises.

4.1.3

12 months ago

4.1.2

1 year ago

4.1.0

1 year ago

4.1.1

1 year ago

3.1.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.0.3

2 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago