@talosjs/socket-client 1.1.0 → 1.1.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 +4 -164
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
1
|
# @talosjs/socket-client
|
|
2
2
|
|
|
3
|
-
WebSocket client for real-time bidirectional communication with automatic reconnection, event handling, and typed message serialization
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-

|
|
9
|
-
|
|
10
|
-
## Features
|
|
11
|
-
|
|
12
|
-
✅ **Socket Class** - WebSocket client with typed send and receive operations via the `Socket` class
|
|
13
|
-
|
|
14
|
-
✅ **Message Queuing** - Automatically queues messages sent before the connection is open and flushes them on connect
|
|
15
|
-
|
|
16
|
-
✅ **Typed Events** - Register handlers for `onMessage`, `onOpen`, `onClose`, and `onError` with typed responses
|
|
17
|
-
|
|
18
|
-
✅ **JSON Serialization** - Automatic JSON serialization for outgoing messages and deserialization for incoming responses
|
|
19
|
-
|
|
20
|
-
✅ **Protocol Auto-Detection** - Automatically converts HTTP/HTTPS URLs to WS/WSS WebSocket protocols
|
|
21
|
-
|
|
22
|
-
✅ **Generic Type Parameters** - Parameterize `Socket<SendData, Response>` for type-safe request and response data
|
|
23
|
-
|
|
24
|
-
✅ **ISocket Interface** - Standard interface defining the WebSocket client contract
|
|
25
|
-
|
|
26
|
-
✅ **Locale Support** - Request data type includes optional language/locale information
|
|
3
|
+
WebSocket client for real-time bidirectional communication with automatic reconnection, event handling, and typed message serialization
|
|
27
4
|
|
|
28
5
|
## Installation
|
|
29
6
|
|
|
@@ -31,147 +8,10 @@ WebSocket client for real-time bidirectional communication with automatic reconn
|
|
|
31
8
|
bun add @talosjs/socket-client
|
|
32
9
|
```
|
|
33
10
|
|
|
34
|
-
##
|
|
35
|
-
|
|
36
|
-
### Basic Connection
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
import { Socket } from '@talosjs/socket-client';
|
|
40
|
-
|
|
41
|
-
const socket = new Socket('wss://api.example.com/ws/chat');
|
|
42
|
-
|
|
43
|
-
socket.onOpen(() => {
|
|
44
|
-
console.log('Connected');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
socket.onMessage((response) => {
|
|
48
|
-
console.log('Received:', response);
|
|
49
|
-
});
|
|
11
|
+
## Documentation
|
|
50
12
|
|
|
51
|
-
|
|
52
|
-
console.log('Disconnected:', event.code, event.reason);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
socket.onError((event) => {
|
|
56
|
-
console.error('Error:', event);
|
|
57
|
-
});
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### Sending Messages
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
import { Socket } from '@talosjs/socket-client';
|
|
64
|
-
|
|
65
|
-
const socket = new Socket('wss://api.example.com/ws/messages');
|
|
66
|
-
|
|
67
|
-
// Messages are queued if the connection is not yet open
|
|
68
|
-
socket.send({
|
|
69
|
-
payload: { message: 'Hello, world!' },
|
|
70
|
-
queries: { room: 'general' }
|
|
71
|
-
});
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### Typed Socket
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
import { Socket, type RequestDataType } from '@talosjs/socket-client';
|
|
78
|
-
|
|
79
|
-
interface ChatRequest extends RequestDataType {
|
|
80
|
-
payload: { message: string; type: 'text' | 'image' };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
interface ChatResponse {
|
|
84
|
-
event: string;
|
|
85
|
-
data: { messageId: string; timestamp: string };
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const socket = new Socket<ChatRequest, ChatResponse>(
|
|
89
|
-
'wss://api.example.com/ws/chat'
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
socket.onMessage((response) => {
|
|
93
|
-
// response is typed as ResponseDataType<ChatResponse>
|
|
94
|
-
console.log(response.data.event);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
socket.send({
|
|
98
|
-
payload: { message: 'Hello!', type: 'text' }
|
|
99
|
-
});
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Closing a Connection
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
import { Socket } from '@talosjs/socket-client';
|
|
106
|
-
|
|
107
|
-
const socket = new Socket('wss://api.example.com/ws');
|
|
108
|
-
|
|
109
|
-
// Close with a status code and reason
|
|
110
|
-
socket.close(1000, 'User disconnected');
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
## API Reference
|
|
114
|
-
|
|
115
|
-
### Classes
|
|
116
|
-
|
|
117
|
-
#### `Socket<SendData, Response>`
|
|
118
|
-
|
|
119
|
-
WebSocket client class with typed messaging.
|
|
120
|
-
|
|
121
|
-
**Constructor:**
|
|
122
|
-
- `url: string` - WebSocket URL (supports `ws://`, `wss://`, `http://`, `https://`, or bare hostnames)
|
|
123
|
-
|
|
124
|
-
**Methods:**
|
|
125
|
-
|
|
126
|
-
| Method | Returns | Description |
|
|
127
|
-
|--------|---------|-------------|
|
|
128
|
-
| `send(data)` | `void` | Send typed data as JSON (queued if not yet connected) |
|
|
129
|
-
| `close(code?, reason?)` | `void` | Close the WebSocket connection |
|
|
130
|
-
| `onMessage(handler)` | `void` | Register a handler for incoming messages |
|
|
131
|
-
| `onOpen(handler)` | `void` | Register a handler for connection open |
|
|
132
|
-
| `onClose(handler)` | `void` | Register a handler for connection close |
|
|
133
|
-
| `onError(handler)` | `void` | Register a handler for errors |
|
|
134
|
-
|
|
135
|
-
### Interfaces
|
|
136
|
-
|
|
137
|
-
#### `ISocket<SendData, Response>`
|
|
138
|
-
|
|
139
|
-
Interface defining the WebSocket client contract.
|
|
140
|
-
|
|
141
|
-
### Types
|
|
142
|
-
|
|
143
|
-
#### `RequestDataType`
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
type RequestDataType = {
|
|
147
|
-
payload?: Record<string, unknown>;
|
|
148
|
-
queries?: Record<string, boolean | number | bigint | string>;
|
|
149
|
-
lang?: LocaleInfoType;
|
|
150
|
-
};
|
|
151
|
-
```
|
|
13
|
+
Read the full documentation at [docs.talosjs.com/advanced/websockets](https://docs.talosjs.com/advanced/websockets).
|
|
152
14
|
|
|
153
15
|
## License
|
|
154
16
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
## Contributing
|
|
158
|
-
|
|
159
|
-
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
160
|
-
|
|
161
|
-
### Development Setup
|
|
162
|
-
|
|
163
|
-
1. Clone the repository
|
|
164
|
-
2. Install dependencies: `bun install`
|
|
165
|
-
3. Run tests: `bun run test`
|
|
166
|
-
4. Build the project: `bun run build`
|
|
167
|
-
|
|
168
|
-
### Guidelines
|
|
169
|
-
|
|
170
|
-
- Write tests for new features
|
|
171
|
-
- Follow the existing code style
|
|
172
|
-
- Update documentation for API changes
|
|
173
|
-
- Ensure all tests pass before submitting PR
|
|
174
|
-
|
|
175
|
-
---
|
|
176
|
-
|
|
177
|
-
Made with ❤️ by the Talos team
|
|
17
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@talosjs/socket-client",
|
|
3
3
|
"description": "WebSocket client for real-time bidirectional communication with automatic reconnection, event handling, and typed message serialization",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"fmt": "bunx biome check --write",
|
|
28
|
+
"lint": "tsgo --noEmit && bunx biome lint"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@talosjs/translation": "^1.1.
|
|
32
|
-
"@talosjs/http-response": "^1.1.
|
|
33
|
-
"@talosjs/app-env": "^1.1.
|
|
31
|
+
"@talosjs/translation": "^1.1.1",
|
|
32
|
+
"@talosjs/http-response": "^1.1.1",
|
|
33
|
+
"@talosjs/app-env": "^1.1.2"
|
|
34
34
|
},
|
|
35
35
|
"keywords": [
|
|
36
36
|
"bun",
|