An open API service indexing awesome lists of open source software.

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.

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.