Options
All
  • Public
  • Public/Protected
  • All
Menu

socketit

socketit

Version Documentation License: MIT GitHub Workflow Status Bundle Size

A stream-based WebSocket library.

🏠 Homepage

Install

yarn add socketit

Usage

Simple server:

const socketit = require("socketit");
const server = new socketit.Server({ port: 8000 });
server.on("connection", (sock) => {
  const socket = new socketit.Socket(sock);
  socket.stream("pos").on("data", (data) => {
    console.log(`x: ${data.x} y: ${data.y}`);
  });
  setTimeout(() => {
    socket.request("position").then((pos) => {
      console.log(`requested position x: ${pos.x} y: ${pos.y}`);
    });
  }, 3000);
});

Simple client:

const socketit = require("socketit");

const ws = new socketit.WebSocket("ws://localhost:8000");
const pos = { x: 0, y: 10 };
ws.on("open", () => {
  const socket = new socketit.Socket(ws);
  socket.handle("position", () => {
    console.log("request for pos");
    return pos;
  });
  setInterval(() => {
    socket.stream("pos").write(pos);
    pos.x += 10;
    pos.y -= 10;
  }, 100);
});

More examples in examples/.

Run tests

yarn test

Browser

You can use socketit in browsers! (With a bundler). Just use the BrowserSocket instead of the Socket.

Documentation

Documentation is available at https://elimerl.github.io/socketit/

Author

👤 elimerl

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

📝 License

Copyright © 2020 elimerl.
This project is MIT licensed.

Generated using TypeDoc