0.1.0 • Published 2 years ago

socket.io-requests v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Socket.io Requests

IN DEVELOPMENT - DO NOT USE!

Better requests functionality for Socket.io - Server & Client.

Socket.io Requests is a package for extending the socket instance of Socket.io for better Acknowledgements (Request - Response) functionality. It creates 2 simple methods for the socket instance.

Features

  • Send requests
  • Listen to requests and respond
  • Set timeout for the request

Installation

Use NPM to install.

npm i socket.io-requests

Configure:

Configure on server

const { Server } = require("socket.io");

const io = new Server({
  /* options */
});

const SioR = require("socket.io-requests");

io.on("connection", (socket) => {
  SioR(socket);
});

Configure on client

const socket = io();
require("socket.io-requests")(socket);

Usage:

Send request

socket.on("connect", () => {
  setTimeout(() => {
    socket
      .sendRequest("request-channel-name", "data of the request")
      .then((res) => {
        console.log("res:", res);
      })
      .catch((err) => {
        console.log("err:", err);
      });
  }, 1000);
});

Listen to request and send response

socket.onRequest("request-channel-name", (data, respond) => {
  const response = {
    hello: "world!",
  };
  respond(response);
});

Send request with custom timeout

  • Timeout is in ms.
  • Default: 30000.
socket
  .sendRequest("request-channel-name", "data of the request", 5000)
  .then((res) => {
    console.log("res:", res);
  })
  .catch((err) => {
    console.log("err:", err);
  });

License

MIT Free Software, Enjoy!

0.1.0

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago