Skip to content

Commit d327402

Browse files
changed server creation added missing index.js
1 parent 2908e8b commit d327402

6 files changed

Lines changed: 197 additions & 19 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Link Protocol Libraries
2+
3+
This repository contains the TypeScript implementations for the Link protocol, a lightweight, multiplexed communication protocol over WebSockets.
4+
5+
## Packages
6+
7+
- **[@marketrix-ai/link-server](./server)**: The server-side library.
8+
- **[@marketrix-ai/link-client](./client)**: The client-side library.
9+
10+
## Getting Started
11+
12+
To install these packages, you must configure your npm client to use the GitHub Package Registry for the `@marketrix-ai` scope.
13+
14+
### 1. Configure Authentication
15+
16+
Create or update an `.npmrc` file in your project root or user home directory (`~/.npmrc`) with the following content:
17+
18+
```ini
19+
@marketrix-ai:registry=https://npm.pkg.github.com
20+
//npm.pkg.github.com/:_authToken=YOUR_GITHUB_PAT
21+
```
22+
23+
Replace `YOUR_GITHUB_PAT` with a GitHub Personal Access Token that has `read:packages` scope.
24+
25+
### 2. Install
26+
27+
Once configured, you can install the packages as usual:
28+
29+
```bash
30+
npm install @marketrix-ai/link-server
31+
npm install @marketrix-ai/link-client
32+
```
33+
34+
## Documentation
35+
36+
Please refer to the README files in each package directory for detailed usage instructions:
37+
38+
- [Server Documentation](./server/README.md)
39+
- [Client Documentation](./client/README.md)

client/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Link Client
2+
3+
This is the client-side library for the Link protocol. It allows you to connect to a Link server and manage multiplexed streams over a single WebSocket connection.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @marketrix-ai/link-client
9+
```
10+
11+
**Note**: You need to configure your `.npmrc` to install packages from the GitHub registry.
12+
13+
### Configuration (`.npmrc`)
14+
15+
Add the following to your `.npmrc` file (either in your project root or user home directory):
16+
17+
```
18+
@marketrix-ai:registry=https://npm.pkg.github.com
19+
```
20+
21+
You will also need to authenticate with GitHub Packages using a Personal Access Token (PAT).
22+
23+
## Usage
24+
25+
```typescript
26+
import { Client } from '@marketrix-ai/link-client';
27+
28+
const client = new Client({
29+
url: 'ws://localhost:8080',
30+
connectionId: 'client-1', // Optional
31+
retryInterval: 1000 // Optional: retry connection every 1s
32+
});
33+
34+
client.connect();
35+
36+
client.on('open', () => {
37+
console.log('Connected to server');
38+
39+
// Create a new stream
40+
const stream = client.createStream('my-stream-1');
41+
42+
stream.on('data', (data) => {
43+
console.log('Received data:', data);
44+
});
45+
46+
stream.send({ message: 'Hello World' });
47+
});
48+
49+
client.on('close', () => {
50+
console.log('Disconnected from server');
51+
});
52+
```

client/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './client';
2+
export * from './stream';
3+
export * from './protocol';

server/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Link Server
2+
3+
This is the server-side library for the Link protocol. It provides a WebSocket server implementation with support for multiplexed streams.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @marketrix-ai/link-server
9+
```
10+
11+
**Note**: You need to configure your `.npmrc` to install packages from the GitHub registry.
12+
13+
### Configuration (`.npmrc`)
14+
15+
Add the following to your `.npmrc` file (either in your project root or user home directory):
16+
17+
```
18+
@marketrix-ai:registry=https://npm.pkg.github.com
19+
```
20+
21+
You will also need to authenticate with GitHub Packages using a Personal Access Token (PAT).
22+
23+
## Usage
24+
25+
You can use the `LinkServer` class to create a WebSocket server or integrate it into an existing application.
26+
27+
### Basic Usage
28+
29+
```typescript
30+
import { LinkServer } from '@marketrix-ai/link-server';
31+
32+
const server = new LinkServer({ port: 8080 });
33+
34+
server.on('listening', () => {
35+
console.log('Server is listening on port 8080');
36+
});
37+
38+
server.on('connection', (connection) => {
39+
console.log('Client connected:', connection.id);
40+
41+
// Handle new streams created by the client
42+
connection.on('newStream', (stream) => {
43+
console.log(`New stream ${stream.id} created`);
44+
45+
stream.on('data', (data) => {
46+
console.log(`Received data on stream ${stream.id}:`, data);
47+
48+
// Example: sending data back
49+
// stream.send({ message: 'Hello from server' });
50+
});
51+
52+
stream.on('close', () => {
53+
console.log(`Stream ${stream.id} closed`);
54+
});
55+
});
56+
57+
connection.on('close', () => {
58+
console.log('Client disconnected');
59+
});
60+
});
61+
```
62+
63+
### Options
64+
65+
The `LinkServer` constructor accepts the same options as the `ws` WebSocket.Server:
66+
67+
```typescript
68+
const server = new LinkServer({
69+
port: 8080,
70+
// host: '0.0.0.0',
71+
// ... other ws options
72+
});
73+
```

server/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './server';
2+
export * from './connection';
3+
export * from './stream';
4+
export * from './protocol';

server/src/server.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1+
import { EventEmitter } from 'events';
12
import WebSocket from 'ws';
23
import { Connection } from './connection';
3-
import { Stream } from './stream';
44

5-
const wss = new WebSocket.Server({ port: 8080 });
5+
export interface LinkServerOptions extends WebSocket.ServerOptions {
6+
// You can add additional options here if needed,
7+
// for now we just extend standard WS server options
8+
}
69

7-
console.log('WebSocket server started on port 8080');
10+
export class LinkServer extends EventEmitter {
11+
private wss: WebSocket.Server;
812

9-
wss.on('connection', (ws: WebSocket) => {
10-
const connection = new Connection(ws);
13+
constructor(options: LinkServerOptions) {
14+
super();
15+
this.wss = new WebSocket.Server(options);
16+
this.init();
17+
}
1118

12-
connection.on('newStream', (stream: Stream) => {
13-
console.log(`New stream created: ${stream.id} on connection ${connection.id}`);
19+
private init() {
20+
this.wss.on('connection', (ws: WebSocket) => {
21+
const connection = new Connection(ws);
22+
this.emit('connection', connection);
23+
});
1424

15-
// Example: Echo back data
16-
stream.on('data', (data: any) => {
17-
console.log(`Received data on stream ${stream.id}:`, data);
18-
// Echoing back
19-
// stream.send({ echoed: data });
25+
this.wss.on('error', (error) => {
26+
this.emit('error', error);
2027
});
2128

22-
stream.on('close', () => {
23-
console.log(`Stream ${stream.id} closed`);
29+
this.wss.on('listening', () => {
30+
this.emit('listening');
2431
});
25-
});
32+
}
2633

27-
connection.on('close', () => {
28-
console.log(`Connection ${connection.id || 'unknown'} closed`);
29-
});
30-
});
34+
public close(cb?: (err?: Error) => void) {
35+
this.wss.close(cb);
36+
}
37+
}

0 commit comments

Comments
 (0)