4.0.0 • Published 2 months ago

pocket-sockets v4.0.0

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

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.

:heavycheck_mark: Support for both _WebSockets and regular TCP sockets with a unified interface.

:heavycheck_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:

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 directory.

Run tests

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

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 directory.

NPM

npm add --save pocket-sockets

Reference

Code documentation and API references are available in the official 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.

4.0.0

2 months ago

3.0.0

3 months ago

2.0.2

11 months ago

2.0.1

11 months ago

1.2.0

2 years ago

2.0.0

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago