https://github.com/jellydn/ws-sockette
The little WebSocket wrapper for nodejs
https://github.com/jellydn/ws-sockette
nodejs websocket ws
Last synced: 3 months ago
JSON representation
The little WebSocket wrapper for nodejs
- Host: GitHub
- URL: https://github.com/jellydn/ws-sockette
- Owner: jellydn
- License: mit
- Created: 2022-03-31T13:26:30.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-11T20:24:23.000Z (about 1 year ago)
- Last Synced: 2025-04-11T21:32:10.793Z (about 1 year ago)
- Topics: nodejs, websocket, ws
- Language: TypeScript
- Homepage:
- Size: 1.32 MB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Welcome to ws-sockette 👋

[](#)
[](https://twitter.com/jellydn)
> The little WebSocket wrapper for nodejs
## Install
```sh
yarn add ws-sockette
```
## Why
[Sockette](https://github.com/lukeed/sockette) is a tiny (367 bytes) wrapper around WebSocket that will automatically reconnect if the connection is lost!
However, sockette doesn't work for nodejs and missing support for [client options](https://github.com/lukeed/sockette/blob/master/src/index.js#L10) for create a websocket client
```typescript
new WebSocket(
address: string | URL,
protocols?: string | string[],
options?: WebSocket.ClientOptions | ClientRequestArgs,
);
```
That's why I take a chance to rewrite with typescript and support nodejs. If you need a tiny wrapper for browser, please use sockette.
## Usage
```typescript
import { wsSockette } from "ws-sockette";
const ws = wsSockette("ws://localhost:3000", {
clientOptions: {
headers: {
Authorization: "Basic YWxhZGRpbjpvcGVuc2VzYW1l",
},
},
timeout: 5e3,
maxAttempts: 10,
onopen: (e) => console.log("Connected!", e),
onmessage: (e) => console.log("Received:", e),
onreconnect: (e) => console.log("Reconnecting...", e),
onmaximum: (e) => console.log("Stop Attempting!", e),
onclose: (e) => console.log("Closed!", e),
onerror: (e) => console.log("Error:", e),
});
ws.send("Hello, world!");
ws.json({ type: "ping" });
ws.close(); // graceful shutdown
// Reconnect 10s later
setTimeout(ws.reconnect, 10e3);
```
## API
```typescript
interface SocketteOptions {
// This is a new option if you compare with sockette
clientOptions?: WebSocket.ClientOptions | ClientRequestArgs;
protocols?: string | string[];
timeout?: number;
maxAttempts?: number;
onopen?: (ev: WebSocket.Event) => any;
onmessage?: (ev: WebSocket.MessageEvent) => any;
onreconnect?: (ev: WebSocket.ErrorEvent | WebSocket.CloseEvent) => any;
onmaximum?: (ev: WebSocket.ErrorEvent | WebSocket.CloseEvent) => any;
onclose?: (ev: WebSocket.CloseEvent) => any;
onerror?: (ev: WebSocket.ErrorEvent) => any;
}
```
Please check [here for complete document](https://github.com/lukeed/sockette/blob/master/readme.md#api)
## Run tests
```sh
yarn test
```
## Author
👤 **Huynh Duc Dung**
- Website: https://productsway.com/
- Twitter: [@jellydn](https://twitter.com/jellydn)
- Github: [@jellydn](https://github.com/jellydn)
## Show your support
Give a ⭐️ if this project helped you!
---
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_