# socket.io-request

> bidirectional request-response for socket.io

Latest version **0.8.0** (published 2018-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install socket.io-request
pnpm add socket.io-request
yarn add socket.io-request
bun add socket.io-request
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2018-02-15 |
| First published | 2016-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 24.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sho Hashimoto |
| Maintainers | shokai |
| Keywords | socket.io, reqres, request, response |

## Links

- npm: https://www.npmjs.com/package/socket.io-request
- Repository: https://github.com/shokai/socket.io-request
- Homepage: https://github.com/shokai/socket.io-request#readme
- Issues: https://github.com/shokai/socket.io-request/issues
- npm.io page: https://npm.io/package/socket.io-request

## Dependencies (3)

- [serialize-error](https://npm.io/package/serialize-error.md) ^2.0.0
- [deserialize-error](https://npm.io/package/deserialize-error.md) ^0.0.3
- [combine-middlewares](https://npm.io/package/combine-middlewares.md) ^0.1.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.8.0 (latest) — 2018-02-15
- 0.7.0 — 2017-09-21
- 0.6.0 — 2017-09-13
- 0.5.3 — 2017-09-11
- 0.5.2 — 2017-09-11
- 0.5.1 — 2017-09-11
- 0.5.0 — 2017-09-10
- 0.4.0 — 2017-09-10
- 0.3.1 — 2016-09-11
- 0.3.0 — 2016-09-10
- 0.2.0 — 2016-06-13
- 0.1.0 — 2016-04-28
- 0.0.4 — 2016-04-06
- 0.0.3 — 2016-04-06
- 0.0.2 — 2016-04-06
- … 1 more at https://npm.io/package/socket.io-request/versions

## README

# socket.io-request

bidirectional request-response for socket.io

- https://github.com/shokai/socket.io-request
- https://npmjs.com/package/socket.io-request

[![Circle CI](https://circleci.com/gh/shokai/socket.io-request.svg?style=svg)](https://circleci.com/gh/shokai/socket.io-request)

## Feature
Of cource, Socket.IO's `emit` and `on` have request-response. This library adds some features.

- Promise interface
- Exception handling
  - `timeout`
  - `disconnect`


## Install

    % npm install socket.io-request -save

## Methods

- `request("method", data)` return `Promise`
- `response("method", handler)`


## Usage

### request from Client to Server

client

```javascript
var ioreq = require("socket.io-request");
var io = require("socket.io-client")("http://localhost:3000");

io.on("connect", function(){
  ioreq(io).request("toUpper", "hello world") // method, data
    .then(function(res){
      console.log(res); // get "HELLO WORLD"
    })
    .catch(function(err){
      console.error(err.stack || err);
    });
});
```

server

```javascript
var ioreq = require("socket.io-request");
var io = require("socket.io")(3000);

io.on("connection", function(socket){ // new client
  ioreq(socket).response("toUpper", function(req, res){ // method, handler
    res(req.toUpperCase()); // return to client
  });
});
```


### request from Server to Client

server

```javascript
var ioreq = require("socket.io-request");
var io = require("socket.io")(3000);

io.on("connection", function(socket){ // new client
  ioreq(io).request("windowSize")
    .then(function(res){
      console.log(res); // get {height: 528, width: 924}
    });
});
```

client (web browser)

```javascript
var ioreq = require("socket.io-request");
var io = require("socket.io-client")("http://localhost:3000");

io.on("connect", function(){
  ioreq(io).response("windowSize", function(req, res){
    res({
      height: window.innerHeight,
      width: window.innerWidth
    }); // return to server
  });
});
```

### Error handling

`res.error` returns error object to requester.

```javascript
ioreq(io).response("foo", function(req, res){
  if(typeof req !== "string") return res.error(new Error("request is not String"));
  res("foo!" + req);
});
```

```javascript
ioreq(io).request("foo", 123)
  .then(function(res){
    console.log(res);
  })
  .catch(function(err){
    console.error(err); // =>  "[Error: request is not String]"
  });
```


### Options

```javascript
var options = {
  event: "socket.io-request", // event name on socket.io
  timeout: 90000              // request timeout (msec)
};

ioreq(io, options).request("foo");
```

## Async-Await

Using [async-await syntax](https://github.com/tc39/ecmascript-asyncawait), you can write like below.

```javascript
async () => {
  const res = await ioreq(io).request("hello");
};
```


## Samples

in `./sample` directory.


## Develop

    % npm i
    % npm run build
    % npm test

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