Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
tcp-websocket
Advanced tools
Readme
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.
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
.
Warning:
TCPWebSocket
class is not fully compatible with theWebSocket
interface, yet.
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 NPM | Issues with Bun |
---|---|
ws | Uses the node:http and node:https to make the request handshake. Source |
websocket | Uses the node:http and node:https to make the request handshake. Source |
websocket-stream | Uses the ws package internally, see ws . Source |
undici | Uses http2 under the hood which is currently not implemented in Bun. Source |
websocket-driver | Works 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. |
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.
FAQs
A WebSocket client-only class made with TCP streams.
The npm package tcp-websocket receives a total of 3 weekly downloads. As such, tcp-websocket popularity was classified as not popular.
We found that tcp-websocket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.