# restocket

> This library allows you to build apis like in express, but that expose a websocket and http(s) endpoint.

Latest version **4.1.3** (published 2023-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install restocket
pnpm add restocket
yarn add restocket
bun add restocket
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.3 |
| Published | 2023-05-05 |
| First published | 2021-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Mark Wylde |
| Maintainers | markwylde |

## Links

- npm: https://www.npmjs.com/package/restocket
- Repository: https://github.com/markwylde/restocket
- Homepage: https://github.com/markwylde/restocket#readme
- Issues: https://github.com/markwylde/restocket/issues
- npm.io page: https://npm.io/package/restocket

## Dependencies (5)

- [events](https://npm.io/package/events.md) ^3.3.0
- [socket.io](https://npm.io/package/socket.io.md) ^4.6.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.2.1
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^4.6.1
- [socketio-wildcard](https://npm.io/package/socketio-wildcard.md) ^2.0.0

## Recent versions

- 4.1.3 (latest) — 2023-05-05
- 4.1.2 — 2023-02-28
- 4.1.1 — 2022-12-28
- 4.1.0 — 2022-11-04
- 4.0.1 — 2022-08-25
- 4.0.0 — 2022-08-25
- 3.1.0 — 2022-08-25
- 3.0.3 — 2021-12-19
- 3.0.2 — 2021-10-09
- 3.0.1 — 2021-06-19
- 3.0.0 — 2021-04-02
- 2.0.0 — 2021-03-31
- 1.0.4 — 2021-03-30
- 1.0.3 — 2021-03-30
- 1.0.2 — 2021-03-30
- … 2 more at https://npm.io/package/restocket/versions

## README

# 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
```javascript
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
```javascript
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:
```json
// 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
```javascript
["GET", "/hello/tester", { "_cid": 1 }]
```

2. Receive the following from the server
```javascript
["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.

---
_Source: https://npm.io/package/restocket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
