0.1.1 • Published 7 months ago

tcp-websocket v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

tcp-websocket

Was originally made to resolve this Bun issue: https://github.com/oven-sh/bun/issues/4529 (still not fixed)

Instead of using node:http or node:https, we use plain TCP sockets to communicate, even for the HTTP request handshake.

Bun support is main priority but it should also support Node. Even though the Node support is kind of experimental.

Getting started

bun add tcp-websocket
import TCPWebSocket from "tcp-websocket";

const ws = new TCPWebSocket("wss://ws.postman-echo.com/raw");

ws.on("open", () => {
  console.info("[open]: connected !\n");
  
  const data = "hello world!";
  ws.send(data);
  console.info("[send::message]:", data);
});

ws.once("message", (event) => {
  console.info("[receive::message]:", event.data);

  console.info("\n[info]: will close in 5 seconds...");
  setTimeout(() => ws.close(), 5_000)
});

ws.on("close", (event) => {
  console.info(`[close(${event.code})]: ${event.reason || "[no reason provided]"}`)
});

You can find more examples @ ./examples.

API

Warning: TCPWebSocket class is not fully compatible with the WebSocket interface, yet.

Why not use those libraries ?

Packages using node:http or node:https to make the request handshake will fail in Bun since their implementation is kinda broken.

Package name on NPMIssues with Bun
wsUses the node:http and node:https to make the request handshake. Source
websocketUses the node:http and node:https to make the request handshake. Source
websocket-streamUses the ws package internally, see ws. Source
undiciUses http2 under the hood which is currently not implemented in Bun. Source
websocket-driverWorks with Bun, but last update was 3 years ago with no TS declarations and ES5 syntax. This package reuses a lot of code from this package.

Development

git clone https://github.com/Vexcited/tcp-websocket

# Install dependencies
bun install

The main source code is located in ./src/index.ts.

You can run the main examples located in ./src/examples using bun run ./examples/simple.ts, for example.

Credits

Resources

0.1.1

7 months ago

0.1.0

8 months ago