@mip-client/ts 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -175
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
# MIP-client-ts
|
|
2
|
-
|
|
3
|
-
TypeScript client for the MIP (MSIP) protocol - handles connections, events, errors, and auto-reconnection.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install mip-client-ts
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
### Basic Connection
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { MIPClient } from "mip-client-ts";
|
|
17
|
-
|
|
18
|
-
const client = new MIPClient({
|
|
19
|
-
host: "127.0.0.1",
|
|
20
|
-
port: 9000,
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// Events
|
|
24
|
-
client.on("connect", () => {
|
|
25
|
-
console.log("Connected to server");
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
client.on("disconnect", () => {
|
|
29
|
-
console.log("Disconnected");
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
client.on("error", (err) => {
|
|
33
|
-
console.error("Error:", err.message);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
client.on("message", (msg) => {
|
|
37
|
-
console.log(`[${msg.topic}] ${msg.message}`);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// Connect
|
|
41
|
-
await client.connect();
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Subscribe / Publish
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
// Subscribe to a topic
|
|
48
|
-
client.subscribe("my-topic");
|
|
49
|
-
|
|
50
|
-
// Listen for messages
|
|
51
|
-
client.on("message", (msg) => {
|
|
52
|
-
console.log(`Topic: ${msg.topic}`);
|
|
53
|
-
console.log(`Message: ${msg.message}`);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
// Publish a message
|
|
57
|
-
client.publish("my-topic", "Hello World!");
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### Advanced Options
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
import { MIPClient, Flags } from "mip-client-ts";
|
|
64
|
-
|
|
65
|
-
const client = new MIPClient({
|
|
66
|
-
host: "127.0.0.1",
|
|
67
|
-
port: 9000,
|
|
68
|
-
autoReconnect: true, // Auto-reconnect (default: true)
|
|
69
|
-
reconnectDelay: 3000, // Delay between reconnections (default: 3000ms)
|
|
70
|
-
maxReconnectAttempts: 10, // Max attempts (0 = infinite)
|
|
71
|
-
pingInterval: 5000, // Automatic ping (0 = disabled)
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Publish with flags
|
|
75
|
-
client.publish("urgent-topic", "Important!", Flags.URGENT | Flags.ACK_REQUIRED);
|
|
76
|
-
|
|
77
|
-
// Listen for ACKs
|
|
78
|
-
client.on("ack", (msgId) => {
|
|
79
|
-
console.log("ACK received for:", msgId.toString());
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Manual ping
|
|
83
|
-
client.ping();
|
|
84
|
-
client.on("pong", () => {
|
|
85
|
-
console.log("Pong received!");
|
|
86
|
-
});
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Auto-Reconnection
|
|
90
|
-
|
|
91
|
-
```typescript
|
|
92
|
-
client.on("reconnecting", (attempt) => {
|
|
93
|
-
console.log(`Reconnection attempt #${attempt}...`);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
client.on("connect", () => {
|
|
97
|
-
// Re-subscribe after reconnection
|
|
98
|
-
client.subscribe("my-topic");
|
|
99
|
-
});
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Factory Function
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
import { createClient } from "mip-client-ts";
|
|
106
|
-
|
|
107
|
-
const client = createClient("127.0.0.1", 9000, {
|
|
108
|
-
autoReconnect: true,
|
|
109
|
-
pingInterval: 10000,
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
## API
|
|
114
|
-
|
|
115
|
-
### `MIPClient`
|
|
116
|
-
|
|
117
|
-
#### Constructor Options
|
|
118
|
-
|
|
119
|
-
| Option | Type | Default | Description |
|
|
120
|
-
| ---------------------- | --------- | ------- | -------------------------------------- |
|
|
121
|
-
| `host` | `string` | - | Server address |
|
|
122
|
-
| `port` | `number` | - | Server port |
|
|
123
|
-
| `autoReconnect` | `boolean` | `true` | Auto-reconnect on disconnect |
|
|
124
|
-
| `reconnectDelay` | `number` | `3000` | Delay between reconnections (ms) |
|
|
125
|
-
| `maxReconnectAttempts` | `number` | `10` |Max reconnection attempts (0 = infinite)|
|
|
126
|
-
| `pingInterval` | `number` | `0` | Ping interval (0 = disabled) |
|
|
127
|
-
|
|
128
|
-
#### Methods
|
|
129
|
-
|
|
130
|
-
- `connect(): Promise<void>` - Connect to server
|
|
131
|
-
- `disconnect(): void` - Disconnect from server
|
|
132
|
-
- `subscribe(topic: string, requireAck?: boolean): bigint` - Subscribe to a topic
|
|
133
|
-
- `unsubscribe(topic: string, requireAck?: boolean): bigint` - Unsubscribe from a topic
|
|
134
|
-
- `publish(topic: string, message: string | Buffer, flags?: number): bigint` - Publish a message
|
|
135
|
-
- `ping(): bigint` - Send a ping
|
|
136
|
-
|
|
137
|
-
#### Events
|
|
138
|
-
|
|
139
|
-
- `connect` - Connection established
|
|
140
|
-
- `disconnect` - Disconnected
|
|
141
|
-
- `reconnecting(attempt)` - Reconnection attempt
|
|
142
|
-
- `message(msg)` - Message received (PUBLISH or EVENT)
|
|
143
|
-
- `event(msg)` - Event received (EVENT only)
|
|
144
|
-
- `ack(msgId)` - ACK received
|
|
145
|
-
- `pong` - Pong received
|
|
146
|
-
- `error(error)` - Error occurred
|
|
147
|
-
- `frame(header, payload)` - Raw frame (advanced usage)
|
|
148
|
-
|
|
149
|
-
### Constants
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
import { FrameType, Flags } from "mip-client-ts";
|
|
153
|
-
|
|
154
|
-
// Frame types
|
|
155
|
-
FrameType.HELLO // 0x0001
|
|
156
|
-
FrameType.SUBSCRIBE // 0x0002
|
|
157
|
-
FrameType.UNSUBSCRIBE // 0x0003
|
|
158
|
-
FrameType.PUBLISH // 0x0004
|
|
159
|
-
FrameType.EVENT // 0x0005
|
|
160
|
-
FrameType.ACK // 0x0006
|
|
161
|
-
FrameType.ERROR // 0x0007
|
|
162
|
-
FrameType.PING // 0x0008
|
|
163
|
-
FrameType.PONG // 0x0009
|
|
164
|
-
FrameType.CLOSE // 0x000A
|
|
165
|
-
|
|
166
|
-
// Flags
|
|
167
|
-
Flags.NONE // 0b00000000
|
|
168
|
-
Flags.ACK_REQUIRED // 0b00000001
|
|
169
|
-
Flags.COMPRESSED // 0b00000010
|
|
170
|
-
Flags.URGENT // 0b00000100
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## License
|
|
174
|
-
|
|
175
|
-
MIT License
|
|
1
|
+
# MIP-client-ts
|
|
2
|
+
|
|
3
|
+
TypeScript client for the MIP (MSIP) protocol - handles connections, events, errors, and auto-reconnection.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mip-client-ts
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Connection
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { MIPClient } from "mip-client-ts";
|
|
17
|
+
|
|
18
|
+
const client = new MIPClient({
|
|
19
|
+
host: "127.0.0.1",
|
|
20
|
+
port: 9000,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Events
|
|
24
|
+
client.on("connect", () => {
|
|
25
|
+
console.log("Connected to server");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
client.on("disconnect", () => {
|
|
29
|
+
console.log("Disconnected");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
client.on("error", (err) => {
|
|
33
|
+
console.error("Error:", err.message);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
client.on("message", (msg) => {
|
|
37
|
+
console.log(`[${msg.topic}] ${msg.message}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Connect
|
|
41
|
+
await client.connect();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Subscribe / Publish
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// Subscribe to a topic
|
|
48
|
+
client.subscribe("my-topic");
|
|
49
|
+
|
|
50
|
+
// Listen for messages
|
|
51
|
+
client.on("message", (msg) => {
|
|
52
|
+
console.log(`Topic: ${msg.topic}`);
|
|
53
|
+
console.log(`Message: ${msg.message}`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Publish a message
|
|
57
|
+
client.publish("my-topic", "Hello World!");
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Advanced Options
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { MIPClient, Flags } from "mip-client-ts";
|
|
64
|
+
|
|
65
|
+
const client = new MIPClient({
|
|
66
|
+
host: "127.0.0.1",
|
|
67
|
+
port: 9000,
|
|
68
|
+
autoReconnect: true, // Auto-reconnect (default: true)
|
|
69
|
+
reconnectDelay: 3000, // Delay between reconnections (default: 3000ms)
|
|
70
|
+
maxReconnectAttempts: 10, // Max attempts (0 = infinite)
|
|
71
|
+
pingInterval: 5000, // Automatic ping (0 = disabled)
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Publish with flags
|
|
75
|
+
client.publish("urgent-topic", "Important!", Flags.URGENT | Flags.ACK_REQUIRED);
|
|
76
|
+
|
|
77
|
+
// Listen for ACKs
|
|
78
|
+
client.on("ack", (msgId) => {
|
|
79
|
+
console.log("ACK received for:", msgId.toString());
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Manual ping
|
|
83
|
+
client.ping();
|
|
84
|
+
client.on("pong", () => {
|
|
85
|
+
console.log("Pong received!");
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Auto-Reconnection
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
client.on("reconnecting", (attempt) => {
|
|
93
|
+
console.log(`Reconnection attempt #${attempt}...`);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
client.on("connect", () => {
|
|
97
|
+
// Re-subscribe after reconnection
|
|
98
|
+
client.subscribe("my-topic");
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Factory Function
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { createClient } from "mip-client-ts";
|
|
106
|
+
|
|
107
|
+
const client = createClient("127.0.0.1", 9000, {
|
|
108
|
+
autoReconnect: true,
|
|
109
|
+
pingInterval: 10000,
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API
|
|
114
|
+
|
|
115
|
+
### `MIPClient`
|
|
116
|
+
|
|
117
|
+
#### Constructor Options
|
|
118
|
+
|
|
119
|
+
| Option | Type | Default | Description |
|
|
120
|
+
| ---------------------- | --------- | ------- | -------------------------------------- |
|
|
121
|
+
| `host` | `string` | - | Server address |
|
|
122
|
+
| `port` | `number` | - | Server port |
|
|
123
|
+
| `autoReconnect` | `boolean` | `true` | Auto-reconnect on disconnect |
|
|
124
|
+
| `reconnectDelay` | `number` | `3000` | Delay between reconnections (ms) |
|
|
125
|
+
| `maxReconnectAttempts` | `number` | `10` |Max reconnection attempts (0 = infinite)|
|
|
126
|
+
| `pingInterval` | `number` | `0` | Ping interval (0 = disabled) |
|
|
127
|
+
|
|
128
|
+
#### Methods
|
|
129
|
+
|
|
130
|
+
- `connect(): Promise<void>` - Connect to server
|
|
131
|
+
- `disconnect(): void` - Disconnect from server
|
|
132
|
+
- `subscribe(topic: string, requireAck?: boolean): bigint` - Subscribe to a topic
|
|
133
|
+
- `unsubscribe(topic: string, requireAck?: boolean): bigint` - Unsubscribe from a topic
|
|
134
|
+
- `publish(topic: string, message: string | Buffer, flags?: number): bigint` - Publish a message
|
|
135
|
+
- `ping(): bigint` - Send a ping
|
|
136
|
+
|
|
137
|
+
#### Events
|
|
138
|
+
|
|
139
|
+
- `connect` - Connection established
|
|
140
|
+
- `disconnect` - Disconnected
|
|
141
|
+
- `reconnecting(attempt)` - Reconnection attempt
|
|
142
|
+
- `message(msg)` - Message received (PUBLISH or EVENT)
|
|
143
|
+
- `event(msg)` - Event received (EVENT only)
|
|
144
|
+
- `ack(msgId)` - ACK received
|
|
145
|
+
- `pong` - Pong received
|
|
146
|
+
- `error(error)` - Error occurred
|
|
147
|
+
- `frame(header, payload)` - Raw frame (advanced usage)
|
|
148
|
+
|
|
149
|
+
### Constants
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { FrameType, Flags } from "mip-client-ts";
|
|
153
|
+
|
|
154
|
+
// Frame types
|
|
155
|
+
FrameType.HELLO // 0x0001
|
|
156
|
+
FrameType.SUBSCRIBE // 0x0002
|
|
157
|
+
FrameType.UNSUBSCRIBE // 0x0003
|
|
158
|
+
FrameType.PUBLISH // 0x0004
|
|
159
|
+
FrameType.EVENT // 0x0005
|
|
160
|
+
FrameType.ACK // 0x0006
|
|
161
|
+
FrameType.ERROR // 0x0007
|
|
162
|
+
FrameType.PING // 0x0008
|
|
163
|
+
FrameType.PONG // 0x0009
|
|
164
|
+
FrameType.CLOSE // 0x000A
|
|
165
|
+
|
|
166
|
+
// Flags
|
|
167
|
+
Flags.NONE // 0b00000000
|
|
168
|
+
Flags.ACK_REQUIRED // 0b00000001
|
|
169
|
+
Flags.COMPRESSED // 0b00000010
|
|
170
|
+
Flags.URGENT // 0b00000100
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT License
|
package/package.json
CHANGED