# pocket-messaging

> A small cryptographic messaging library written in TypeScript both for browser and nodejs supporting TCP and WebSockets

Latest version **6.0.1** (published 2024-04-09) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 25/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2024-04-09 |
| First published | 2022-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 4 |
| Unpacked size | 243.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Thomas Backlund |
| Maintainers | bashlund |
| Keywords | messaging, message, send, data, trusted, peer, p2p, public-key, cryptography, tls |

## Links

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

## Dependencies (4)

- [eventemitter3](https://npm.io/package/eventemitter3.md) ^4.0.7
- [pocket-console](https://npm.io/package/pocket-console.md) 0.5.0
- [pocket-sockets](https://npm.io/package/pocket-sockets.md) 4.0.0
- [libsodium-wrappers](https://npm.io/package/libsodium-wrappers.md) ^0.7.9

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 6.0.1 (latest) — 2024-04-09
- 6.0.0 — 2024-03-08
- 5.0.0 — 2024-02-26
- 4.0.0 — 2024-02-21
- 3.0.0 — 2023-06-28
- 2.1.0 — 2023-02-08
- 2.0.0 — 2022-12-09
- 1.1.0 — 2022-10-10
- 1.0.2 — 2022-05-16
- 1.0.1 — 2022-05-16

## README

# pocket-messaging

A small, eventdriven cryptographic messaging library written in TypeScript for client/server communication over TCP or WebSockets with support for TLS encryption.

Runs in browser and NodeJS.

## Background
This is an event-driven cryptographic (Ed25519/Sodium) communications library based on top of `pocket-sockets` which implements the SSB handshake protocol with some added optional bytes for data exchange.

Rationale:  

    - Using `pocket-sockets` to have a uniform interface for using both TCP sockets and WebSockets in the application.
    - Ed25519 handshake where server public key is the known part (the SSB 4-way handshake protocol).
    - Sodium stream encryption.
    - The pocket-sockets layer does also support TLS encryption in the sockets layer.
    - `pocket-messaging` brings a request/response (indefinite) cycle of communications between peers. This can make application code very sleak.



## Example
For a quick glimpse of what it looks like to set up two participants exchanging call and response messages and then finalizing the connections, follow the example below:
```javascript
let [socket1, socket2] = CreatePair();
let messaging1 = new Messaging(socket1);
let messaging2 = new Messaging(socket2);

messaging1.open();
messaging2.open();

// Send message A from participant #1 to participant #2, then close upon reply
(async function() {
    const data = Buffer.from("A");
    const eventEmitter = messaging1.send("ping", data, 10000, true);
    if(eventEmitter) {
        const reply = await once(eventEmitter, "reply");
    }
    messaging1.close();
}) ();

// Send message B from participant #2 to participant #1, then close upon reply
(async function() {
    const eventEmitter = messaging2.getEventEmitter();
    const event = await once(eventEmitter, "route");
    const data = Buffer.from("B");
    const eventEmitterSend = messaging2.send(event.fromMsgId, data, 10000);
    if(eventEmitterSend) {
        const reply = await once(eventEmitterSend, "mixed");
    }
    messaging2.close();
}) ();
```

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

## Reference
Code documentation and API references are available in the official [Wiki](https://github.com/bashlund/pocket-messaging/wiki): [https://github.com/bashlund/pocket-messaging/wiki](https://github.com/bashlund/pocket-messaging/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-messaging · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
