https://github.com/wkhallen/dtp.js
Asynchronous event-driven networking interfaces for JavaScript and TypeScript.
https://github.com/wkhallen/dtp.js
async javascript networking socket socket-client socket-server typescript
Last synced: 4 months ago
JSON representation
Asynchronous event-driven networking interfaces for JavaScript and TypeScript.
- Host: GitHub
- URL: https://github.com/wkhallen/dtp.js
- Owner: WKHAllen
- License: mit
- Created: 2020-11-03T03:29:38.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-22T01:20:45.000Z (almost 3 years ago)
- Last Synced: 2025-03-22T08:30:33.634Z (9 months ago)
- Topics: async, javascript, networking, socket, socket-client, socket-server, typescript
- Language: TypeScript
- Homepage: https://wkhallen.com/dtp
- Size: 211 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Data Transfer Protocol for JavaScript/TypeScript
Asynchronous event-driven networking interfaces for JavaScript and TypeScript.
## Data Transfer Protocol
The Data Transfer Protocol (DTP) is a larger project to make ergonomic network programming available in any language.
See the full project [here](https://wkhallen.com/dtp/).
## Installation
Install the package:
```sh
$ npm install --save dtp.js
```
## Creating a server
A server can be built using the `Server` implementation:
```ts
import {Server} from "dtp.js";
// Create a server that receives strings and returns the length of each string
const server = new Server();
server.on("connect", (clientID) => {
console.log(`Client with ID ${clientID} connected`);
});
server.on("disconnect", (clientID) => {
console.log(`Client with ID ${clientID} disconnected`);
});
server.on("receive", async (clientID, data) => {
// Send back the length of the string
await server.send(data.length, clientID);
});
// Start the server
await server.start({host: "0.0.0.0", port: 29275});
```
## Creating a client
A client can be built using the `Client` implementation:
```ts
import {Client} from "dtp.js";
import * as assert from "assert";
// A message to send to the server
const message = "Hello, server!";
// Create a client that sends a message to the server and receives the length of the message
const client = new Client();
client.on("receive", (data) => {
// Validate the response
console.log(`Received response from server: ${data}`);
assert.strictEqual(data, message.length);
});
client.on("disconnected", () => {
console.error("Unexpectedly disconnected from server");
});
// Connect to the server
await client.connect({host: "127.0.0.1", port: 29275});
// Send the message to the server
await client.send(message);
```
## Security
Information security comes included. Every message sent over a network interface is encrypted with AES-256. Key
exchanges are performed using a 4096-bit RSA key-pair.