# pocket-sockets

> A powerful and smooth client/server sockets library for browser and NodeJS. Supports both WebSockets and regular TCP sockets, TLS encrypted or not.

Latest version **4.1.0** (published 2025-08-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install pocket-sockets
pnpm add pocket-sockets
yarn add pocket-sockets
bun add pocket-sockets
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2025-08-07 |
| First published | 2021-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 1 |
| Unpacked size | 384.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Thomas Backlund |
| Maintainers | bashlund |
| Keywords | sockets, library, browser, nodejs, websockets, raw, tcp, encrypted, encryption, networking, communication, layer |

## Links

- npm: https://www.npmjs.com/package/pocket-sockets
- Repository: https://github.com/bashlund/pocket-sockets
- Homepage: https://github.com/bashlund/pocket-sockets#readme
- Issues: https://github.com/bashlund/pocket-sockets/issues
- npm.io page: https://npm.io/package/pocket-sockets

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) ^8.16.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2025-08-07
- 4.0.0 — 2024-03-07
- 3.0.0 — 2024-02-20
- 2.0.2 — 2023-06-28
- 2.0.1 — 2023-06-28
- 2.0.0 — 2022-12-09
- 1.2.0 — 2022-10-10
- 1.1.1 — 2022-05-16
- 1.1.0 — 2022-05-16
- 1.0.1 — 2021-09-28

## README

# pocket-sockets

A powerful and smooth client/server sockets library for browser and _Node.js_, written in TypeScript with very few dependencies.

:heavy_check_mark: Written in TypeScript.  

:heavy_check_mark: Support for both _WebSockets_ and regular _TCP_ sockets with a unified interface.  

:heavy_check_mark: Works both in _NodeJS_ and browser.  

:heavy_check_mark: Supports SSL/TLS encryption with certificates.  

:heavy_check_mark: Test suite of 105 tests.  

## WebSockets vs. regular TCP sockets
_WebSockets_ are a must when using a browser, however plain _TCP_ sockets are faster and a good choice when no browser is involved.

The overall interface for _pocket-sockets_ _WebSocket_ and _TCP_ sockets **are identical** so it is easy to switch between the underlying implementations.

## Example
For a quick glimpse of what it looks like to set up a server that receives a string from clients, then replies back and closes the connection afterwards, follow the example below:
```typescript
import {WSServer, WSClient, ClientInterface} from "pocket-sockets";

const server = new WSServer({
    host: "localhost",
    port: 8181
});
server.listen();

server.onConnection( (client: ClientInterface) => {
    client.onData( (data: Buffer | string) => {
        client.send("This is server: received!");
    });
    client.onClose( () => {
        server.close();
    });
});

const client = new WSClient({
    host: "localhost",
    port: 8181
});
client.connect();

client.onConnect( () => {
    client.onData( (data: Buffer | string) => {
        client.close();
    });
    client.send("This is client: hello");
});
```

For complete examples, please refer to the files under the [./example](https://github.com/bashlund/pocket-sockets/tree/main/example) directory.

## Run tests
```sh
git clone https://github.com/bashlund/pocket-sockets.git
cd pocket-sockets
npm isntall
cd ./test/cert/ && ./generate_self_signed_cert.sh && cd ../..
npm test
```

## Run examples
```sh
git clone https://github.com/bashlund/pocket-sockets.git
cd pocket-sockets
npm isntall
npx ts-node ./example/example-ws.ts
npx ts-node ./example/example-tcp.ts
```

## Use in browser
For browser examples, please refer to the files under the [./example](https://github.com/bashlund/pocket-sockets/tree/main/example) directory.

## NPM
```sh
npm add --save pocket-sockets
```

## Reference
Code documentation and API references are available in the official [Wiki](https://github.com/bashlund/pocket-sockets/wiki): [https://github.com/bashlund/pocket-sockets/wiki](https://github.com/bashlund/pocket-sockets/wiki).

## Credits
Lib written by @bashlund, tests and wiki nicely crafted by @filippsen.

## License
This project is released under the _MIT_ license.

---
_Source: https://npm.io/package/pocket-sockets · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
