Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tcp-websocket

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tcp-websocket

A WebSocket client-only class made with TCP streams.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-84.62%
Maintainers
0
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 17 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc