@venizia/ignis-docs 0.0.5 → 0.0.6-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/package.json +1 -1
- package/wiki/best-practices/architectural-patterns.md +0 -2
- package/wiki/best-practices/architecture-decisions.md +0 -8
- package/wiki/best-practices/code-style-standards/control-flow.md +1 -1
- package/wiki/best-practices/code-style-standards/index.md +0 -1
- package/wiki/best-practices/code-style-standards/tooling.md +0 -3
- package/wiki/best-practices/contribution-workflow.md +12 -12
- package/wiki/best-practices/index.md +4 -14
- package/wiki/best-practices/performance-optimization.md +3 -3
- package/wiki/best-practices/security-guidelines.md +2 -2
- package/wiki/best-practices/troubleshooting-tips.md +1 -1
- package/wiki/guides/core-concepts/application/bootstrapping.md +6 -7
- package/wiki/guides/core-concepts/components-guide.md +1 -1
- package/wiki/guides/core-concepts/components.md +2 -2
- package/wiki/guides/core-concepts/dependency-injection.md +4 -5
- package/wiki/guides/core-concepts/persistent/datasources.md +4 -5
- package/wiki/guides/core-concepts/services.md +1 -1
- package/wiki/guides/get-started/5-minute-quickstart.md +4 -5
- package/wiki/guides/get-started/philosophy.md +12 -24
- package/wiki/guides/index.md +2 -9
- package/wiki/guides/reference/mcp-docs-server.md +13 -13
- package/wiki/guides/tutorials/building-a-crud-api.md +10 -10
- package/wiki/guides/tutorials/complete-installation.md +11 -12
- package/wiki/guides/tutorials/ecommerce-api.md +3 -3
- package/wiki/guides/tutorials/realtime-chat.md +6 -6
- package/wiki/guides/tutorials/testing.md +4 -5
- package/wiki/index.md +8 -14
- package/wiki/references/base/bootstrapping.md +0 -3
- package/wiki/references/base/components.md +2 -2
- package/wiki/references/base/controllers.md +0 -1
- package/wiki/references/base/datasources.md +1 -1
- package/wiki/references/base/dependency-injection.md +2 -2
- package/wiki/references/base/filter-system/default-filter.md +2 -3
- package/wiki/references/base/filter-system/index.md +1 -1
- package/wiki/references/base/filter-system/quick-reference.md +0 -14
- package/wiki/references/base/middlewares.md +0 -8
- package/wiki/references/base/providers.md +0 -9
- package/wiki/references/base/repositories/advanced.md +1 -1
- package/wiki/references/base/repositories/mixins.md +2 -3
- package/wiki/references/base/services.md +0 -1
- package/wiki/references/components/authentication/api.md +444 -0
- package/wiki/references/components/authentication/errors.md +177 -0
- package/wiki/references/components/authentication/index.md +571 -0
- package/wiki/references/components/authentication/usage.md +781 -0
- package/wiki/references/components/health-check.md +292 -103
- package/wiki/references/components/index.md +14 -12
- package/wiki/references/components/mail/api.md +505 -0
- package/wiki/references/components/mail/errors.md +176 -0
- package/wiki/references/components/mail/index.md +535 -0
- package/wiki/references/components/mail/usage.md +404 -0
- package/wiki/references/components/request-tracker.md +229 -25
- package/wiki/references/components/socket-io/api.md +1051 -0
- package/wiki/references/components/socket-io/errors.md +119 -0
- package/wiki/references/components/socket-io/index.md +410 -0
- package/wiki/references/components/socket-io/usage.md +322 -0
- package/wiki/references/components/static-asset/api.md +261 -0
- package/wiki/references/components/static-asset/errors.md +89 -0
- package/wiki/references/components/static-asset/index.md +617 -0
- package/wiki/references/components/static-asset/usage.md +364 -0
- package/wiki/references/components/swagger.md +390 -110
- package/wiki/references/components/template/api-page.md +125 -0
- package/wiki/references/components/template/errors-page.md +100 -0
- package/wiki/references/components/template/index.md +104 -0
- package/wiki/references/components/template/setup-page.md +134 -0
- package/wiki/references/components/template/single-page.md +132 -0
- package/wiki/references/components/template/usage-page.md +127 -0
- package/wiki/references/components/websocket/api.md +508 -0
- package/wiki/references/components/websocket/errors.md +123 -0
- package/wiki/references/components/websocket/index.md +453 -0
- package/wiki/references/components/websocket/usage.md +475 -0
- package/wiki/references/helpers/cron/index.md +224 -0
- package/wiki/references/helpers/crypto/index.md +537 -0
- package/wiki/references/helpers/env/index.md +214 -0
- package/wiki/references/helpers/error/index.md +232 -0
- package/wiki/references/helpers/index.md +16 -15
- package/wiki/references/helpers/inversion/index.md +608 -0
- package/wiki/references/helpers/logger/index.md +600 -0
- package/wiki/references/helpers/network/api.md +986 -0
- package/wiki/references/helpers/network/index.md +620 -0
- package/wiki/references/helpers/queue/index.md +589 -0
- package/wiki/references/helpers/redis/index.md +495 -0
- package/wiki/references/helpers/socket-io/api.md +497 -0
- package/wiki/references/helpers/socket-io/index.md +513 -0
- package/wiki/references/helpers/storage/api.md +705 -0
- package/wiki/references/helpers/storage/index.md +583 -0
- package/wiki/references/helpers/template/index.md +66 -0
- package/wiki/references/helpers/template/single-page.md +126 -0
- package/wiki/references/helpers/testing/index.md +510 -0
- package/wiki/references/helpers/types/index.md +512 -0
- package/wiki/references/helpers/uid/index.md +272 -0
- package/wiki/references/helpers/websocket/api.md +736 -0
- package/wiki/references/helpers/websocket/index.md +574 -0
- package/wiki/references/helpers/worker-thread/index.md +470 -0
- package/wiki/references/index.md +2 -9
- package/wiki/references/quick-reference.md +3 -18
- package/wiki/references/utilities/jsx.md +1 -8
- package/wiki/references/utilities/statuses.md +0 -7
- package/wiki/references/components/authentication.md +0 -476
- package/wiki/references/components/mail.md +0 -687
- package/wiki/references/components/socket-io.md +0 -562
- package/wiki/references/components/static-asset.md +0 -1277
- package/wiki/references/helpers/cron.md +0 -108
- package/wiki/references/helpers/crypto.md +0 -132
- package/wiki/references/helpers/env.md +0 -83
- package/wiki/references/helpers/error.md +0 -97
- package/wiki/references/helpers/inversion.md +0 -176
- package/wiki/references/helpers/logger.md +0 -296
- package/wiki/references/helpers/network.md +0 -396
- package/wiki/references/helpers/queue.md +0 -150
- package/wiki/references/helpers/redis.md +0 -142
- package/wiki/references/helpers/socket-io.md +0 -932
- package/wiki/references/helpers/storage.md +0 -665
- package/wiki/references/helpers/testing.md +0 -133
- package/wiki/references/helpers/types.md +0 -167
- package/wiki/references/helpers/uid.md +0 -167
- package/wiki/references/helpers/worker-thread.md +0 -178
- package/wiki/references/src-details/boot.md +0 -379
- package/wiki/references/src-details/core.md +0 -263
- package/wiki/references/src-details/dev-configs.md +0 -298
- package/wiki/references/src-details/docs.md +0 -71
- package/wiki/references/src-details/helpers.md +0 -211
- package/wiki/references/src-details/index.md +0 -86
- package/wiki/references/src-details/inversion.md +0 -340
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
# Socket.IO -- API Reference
|
|
2
|
+
|
|
3
|
+
> Full method signatures, type definitions, and constants for `SocketIOServerHelper` and `SocketIOClientHelper`.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
SocketIOServerHelper (extends BaseHelper)
|
|
9
|
+
|
|
|
10
|
+
|-- configure()
|
|
11
|
+
| |-- waitForRedisReady() x3 (pub, sub, emitter)
|
|
12
|
+
| |-- initIOServer() (Node.js HTTPServer or Bun engine)
|
|
13
|
+
| |-- io.adapter(createAdapter(redisPub, redisSub))
|
|
14
|
+
| |-- new Emitter(redisEmitter)
|
|
15
|
+
| +-- io.on('connection', onClientConnect)
|
|
16
|
+
|
|
|
17
|
+
|-- onClientConnect({ socket })
|
|
18
|
+
| |-- Create ISocketIOClient (state: UNAUTHORIZED)
|
|
19
|
+
| |-- Start authenticateTimeout timer
|
|
20
|
+
| |-- Register 'disconnect' handler
|
|
21
|
+
| +-- Register 'authenticate' handler
|
|
22
|
+
| |-- authenticateFn(handshake)
|
|
23
|
+
| +-- onClientAuthenticated({ socket })
|
|
24
|
+
| |-- State -> AUTHENTICATED
|
|
25
|
+
| |-- Join defaultRooms
|
|
26
|
+
| |-- Register room handlers (join/leave)
|
|
27
|
+
| |-- Start ping interval
|
|
28
|
+
| |-- Emit 'authenticated' to client
|
|
29
|
+
| +-- Invoke clientConnectedFn
|
|
30
|
+
|
|
|
31
|
+
|-- send({ destination?, payload, doLog?, cb? })
|
|
32
|
+
| +-- emitter.compress(true).to(destination).emit(topic, data)
|
|
33
|
+
|
|
|
34
|
+
+-- shutdown()
|
|
35
|
+
|-- Disconnect all clients
|
|
36
|
+
|-- io.close()
|
|
37
|
+
+-- Quit Redis clients (pub, sub, emitter)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
SocketIOClientHelper (extends BaseHelper)
|
|
41
|
+
|
|
|
42
|
+
|-- constructor -> configure()
|
|
43
|
+
| |-- io(host, options)
|
|
44
|
+
| |-- Register 'connect' handler
|
|
45
|
+
| |-- Register 'disconnect' handler
|
|
46
|
+
| |-- Register 'connect_error' handler
|
|
47
|
+
| |-- Register 'authenticated' handler
|
|
48
|
+
| |-- Register 'unauthenticated' handler
|
|
49
|
+
| +-- Register 'ping' handler
|
|
50
|
+
|
|
|
51
|
+
|-- authenticate()
|
|
52
|
+
| +-- client.emit('authenticate')
|
|
53
|
+
|
|
|
54
|
+
|-- subscribe({ event, handler, ignoreDuplicate? })
|
|
55
|
+
|-- emit({ topic, data, doLog?, cb? })
|
|
56
|
+
|-- joinRooms({ rooms }) -> client.emit('join', { rooms })
|
|
57
|
+
|-- leaveRooms({ rooms }) -> client.emit('leave', { rooms })
|
|
58
|
+
|
|
|
59
|
+
+-- shutdown()
|
|
60
|
+
|-- client.removeAllListeners()
|
|
61
|
+
|-- client.disconnect()
|
|
62
|
+
+-- state -> UNAUTHORIZED
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Server API
|
|
66
|
+
|
|
67
|
+
### `SocketIOServerHelper`
|
|
68
|
+
|
|
69
|
+
Extends `BaseHelper`. Manages a Socket.IO server with Redis adapter, authentication, room management, and heartbeat pings.
|
|
70
|
+
|
|
71
|
+
#### `constructor(opts: TSocketIOServerOptions)`
|
|
72
|
+
|
|
73
|
+
Creates the server helper. Validates the runtime-specific server/engine and initializes three Redis client connections by duplicating the provided `redisConnection`.
|
|
74
|
+
|
|
75
|
+
Does **not** start the IO server -- call `configure()` to complete initialization.
|
|
76
|
+
|
|
77
|
+
#### `configure(): Promise<void>`
|
|
78
|
+
|
|
79
|
+
Initializes the Socket.IO server and sets up Redis infrastructure:
|
|
80
|
+
|
|
81
|
+
1. Ensures all three Redis clients (pub, sub, emitter) are connected and ready
|
|
82
|
+
2. Creates the `IOServer` based on runtime (`new IOServer(httpServer, serverOptions)` for Node.js, or `new IOServer()` with `io.bind(engine)` for Bun)
|
|
83
|
+
3. Attaches the Redis adapter via `@socket.io/redis-adapter`
|
|
84
|
+
4. Creates a Redis emitter via `@socket.io/redis-emitter`
|
|
85
|
+
5. Registers the `'connection'` event handler
|
|
86
|
+
|
|
87
|
+
Must be called before `on()`, `send()`, or any server operations.
|
|
88
|
+
|
|
89
|
+
#### `getIOServer(): IOServer`
|
|
90
|
+
|
|
91
|
+
Returns the underlying `socket.io` `Server` instance for direct access.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const io = socketServer.getIOServer();
|
|
95
|
+
io.of('/admin').on('connection', (socket) => { /* ... */ });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### `getEngine(): any`
|
|
99
|
+
|
|
100
|
+
Returns the Bun engine instance. Throws if the runtime is not `'bun'`.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
// Error: '[getEngine] Engine is only available for Bun runtime!'
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### `getClients(opts?: { id?: string }): ISocketIOClient | Map<string, ISocketIOClient> | undefined`
|
|
107
|
+
|
|
108
|
+
Returns client information.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Get all clients
|
|
112
|
+
const allClients = socketServer.getClients() as Map<string, ISocketIOClient>;
|
|
113
|
+
|
|
114
|
+
// Get a specific client by socket ID
|
|
115
|
+
const client = socketServer.getClients({ id: 'socket-id' }) as ISocketIOClient | undefined;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### `on<HandlerArgsType, HandlerReturnType>(opts: { topic: string; handler: (...args: HandlerArgsType) => ValueOrPromise<HandlerReturnType> }): void`
|
|
119
|
+
|
|
120
|
+
Registers an event handler on the IO server instance.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
socketServer.on({
|
|
124
|
+
topic: 'custom-event',
|
|
125
|
+
handler: (data: { userId: string }) => {
|
|
126
|
+
console.log('Received:', data);
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Throws:**
|
|
132
|
+
- `'[on] Invalid topic to start binding handler'` -- if `topic` is empty/falsy
|
|
133
|
+
- `'[on] Invalid event handler | topic: {topic}'` -- if `handler` is missing
|
|
134
|
+
- `'[on] IOServer is not initialized yet!'` -- if called before `configure()`
|
|
135
|
+
|
|
136
|
+
#### `onClientConnect(opts: { socket: IOSocket }): void`
|
|
137
|
+
|
|
138
|
+
Handles a new socket connection. Called automatically by the `'connection'` event. Can also be called manually.
|
|
139
|
+
|
|
140
|
+
1. Creates an `ISocketIOClient` entry with state `UNAUTHORIZED`
|
|
141
|
+
2. Starts the `authenticateTimeout` timer
|
|
142
|
+
3. Registers `'disconnect'` and `'authenticate'` handlers on the socket
|
|
143
|
+
|
|
144
|
+
Returns early (no-op) if `socket` is falsy or the client ID already exists.
|
|
145
|
+
|
|
146
|
+
#### `onClientAuthenticated(opts: { socket: IOSocket }): void`
|
|
147
|
+
|
|
148
|
+
Called after successful authentication. Can also be called manually to programmatically authenticate a client.
|
|
149
|
+
|
|
150
|
+
1. Sets client state to `AUTHENTICATED`
|
|
151
|
+
2. Sends an initial ping
|
|
152
|
+
3. Joins all `defaultRooms`
|
|
153
|
+
4. Registers room handlers (`join`, `leave`)
|
|
154
|
+
5. Starts the periodic ping interval
|
|
155
|
+
6. Emits `'authenticated'` event to the client with `{ id, time }`
|
|
156
|
+
7. Invokes `clientConnectedFn` callback (errors caught and logged)
|
|
157
|
+
|
|
158
|
+
#### `ping(opts: { socket: IOSocket; doIgnoreAuth: boolean }): void`
|
|
159
|
+
|
|
160
|
+
Sends a `'ping'` event to the client with `{ time: ISO string }`.
|
|
161
|
+
|
|
162
|
+
- If `doIgnoreAuth` is `false` and the client is not in `AUTHENTICATED` state, the client is disconnected
|
|
163
|
+
- If the socket or client is not found, returns silently
|
|
164
|
+
|
|
165
|
+
#### `disconnect(opts: { socket: IOSocket }): void`
|
|
166
|
+
|
|
167
|
+
Disconnects a client and cleans up internal state:
|
|
168
|
+
|
|
169
|
+
1. Clears the ping `interval` timer
|
|
170
|
+
2. Clears the `authenticateTimeout` timer
|
|
171
|
+
3. Removes the client from the `clients` map
|
|
172
|
+
4. Calls `socket.disconnect()`
|
|
173
|
+
|
|
174
|
+
#### `send(opts: { destination?: string; payload: { topic: string; data: any }; doLog?: boolean; cb?: () => void }): void`
|
|
175
|
+
|
|
176
|
+
Emits a message via the Redis emitter with compression enabled.
|
|
177
|
+
|
|
178
|
+
| Parameter | Type | Description |
|
|
179
|
+
|-----------|------|-------------|
|
|
180
|
+
| `destination` | `string \| undefined` | Socket ID or room name. If omitted, broadcasts to all |
|
|
181
|
+
| `payload.topic` | `string` | Event name |
|
|
182
|
+
| `payload.data` | `any` | Event payload |
|
|
183
|
+
| `doLog` | `boolean` | If `true`, logs the message details. Default: `false` |
|
|
184
|
+
| `cb` | `() => void` | Callback invoked asynchronously via `setImmediate` after emission |
|
|
185
|
+
|
|
186
|
+
Returns early (no-op) if `payload`, `topic`, or `data` is falsy.
|
|
187
|
+
|
|
188
|
+
#### `shutdown(): Promise<void>`
|
|
189
|
+
|
|
190
|
+
Gracefully shuts down the server:
|
|
191
|
+
|
|
192
|
+
1. Disconnects all tracked clients (clears their intervals and timeouts)
|
|
193
|
+
2. Clears the `clients` map
|
|
194
|
+
3. Closes the IO server
|
|
195
|
+
4. Quits all three Redis connections (pub, sub, emitter)
|
|
196
|
+
|
|
197
|
+
## Client API
|
|
198
|
+
|
|
199
|
+
### `SocketIOClientHelper`
|
|
200
|
+
|
|
201
|
+
Extends `BaseHelper`. Manages a Socket.IO client connection with authentication, event subscriptions, and room operations.
|
|
202
|
+
|
|
203
|
+
#### `constructor(opts: ISocketIOClientOptions)`
|
|
204
|
+
|
|
205
|
+
Creates and immediately configures the client. The constructor calls `configure()` internally, which establishes the connection and registers lifecycle event handlers.
|
|
206
|
+
|
|
207
|
+
#### `configure(): void`
|
|
208
|
+
|
|
209
|
+
Initializes the `socket.io-client` connection and registers internal event handlers. Called automatically by the constructor. If called again when a client already exists, returns early (no-op).
|
|
210
|
+
|
|
211
|
+
Registered handlers:
|
|
212
|
+
- `'connect'` -- invokes `onConnected` callback
|
|
213
|
+
- `'disconnect'` -- resets state to `UNAUTHORIZED`, invokes `onDisconnected` callback
|
|
214
|
+
- `'connect_error'` -- invokes `onError` callback
|
|
215
|
+
- `'authenticated'` -- sets state to `AUTHENTICATED`, invokes `onAuthenticated` callback
|
|
216
|
+
- `'unauthenticated'` -- resets state to `UNAUTHORIZED`, invokes `onUnauthenticated` callback
|
|
217
|
+
- `'ping'` -- logs debug message
|
|
218
|
+
|
|
219
|
+
#### `getState(): TSocketIOClientState`
|
|
220
|
+
|
|
221
|
+
Returns the current authentication state: `'unauthorized'`, `'authenticating'`, or `'authenticated'`.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const state = client.getState();
|
|
225
|
+
if (state === 'authenticated') {
|
|
226
|
+
client.emit({ topic: 'message', data: { text: 'hello' } });
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### `getSocketClient(): Socket`
|
|
231
|
+
|
|
232
|
+
Returns the underlying `socket.io-client` `Socket` instance for direct access.
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
const rawSocket = client.getSocketClient();
|
|
236
|
+
rawSocket.io.opts.reconnection = false;
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### `authenticate(): void`
|
|
240
|
+
|
|
241
|
+
Initiates the authentication handshake by emitting the `'authenticate'` event to the server.
|
|
242
|
+
|
|
243
|
+
- Does nothing if the client is not connected
|
|
244
|
+
- Does nothing if the current state is not `'unauthorized'`
|
|
245
|
+
- Sets state to `AUTHENTICATING` before emitting
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
// Typically called inside the onConnected callback
|
|
249
|
+
const client = new SocketIOClientHelper({
|
|
250
|
+
// ...
|
|
251
|
+
onConnected: () => {
|
|
252
|
+
client.authenticate();
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### `subscribe<T>(opts: { event: string; handler: TSocketIOEventHandler<T>; ignoreDuplicate?: boolean }): void`
|
|
258
|
+
|
|
259
|
+
Registers an event handler on the client socket. The handler is automatically wrapped with error handling (catches both sync throws and async rejections).
|
|
260
|
+
|
|
261
|
+
| Parameter | Type | Default | Description |
|
|
262
|
+
|-----------|------|---------|-------------|
|
|
263
|
+
| `event` | `string` | -- | Event name to listen for |
|
|
264
|
+
| `handler` | `TSocketIOEventHandler<T>` | -- | Callback receiving the event data |
|
|
265
|
+
| `ignoreDuplicate` | `boolean` | `true` | If `true` and a handler already exists for this event, skips registration |
|
|
266
|
+
|
|
267
|
+
#### `subscribeMany(opts: { events: Record<string, TSocketIOEventHandler>; ignoreDuplicate?: boolean }): void`
|
|
268
|
+
|
|
269
|
+
Batch-registers multiple event handlers. Calls `subscribe()` for each entry.
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
client.subscribeMany({
|
|
273
|
+
events: {
|
|
274
|
+
'event-a': (data) => { /* ... */ },
|
|
275
|
+
'event-b': (data) => { /* ... */ },
|
|
276
|
+
},
|
|
277
|
+
ignoreDuplicate: false,
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### `unsubscribe(opts: { event: string; handler?: TSocketIOEventHandler }): void`
|
|
282
|
+
|
|
283
|
+
Removes event handlers from the client socket.
|
|
284
|
+
|
|
285
|
+
- If `handler` is provided, removes only that specific handler
|
|
286
|
+
- If `handler` is omitted, removes **all** handlers for the event
|
|
287
|
+
- If no listeners exist for the event, returns early (no-op)
|
|
288
|
+
|
|
289
|
+
#### `unsubscribeMany(opts: { events: string[] }): void`
|
|
290
|
+
|
|
291
|
+
Batch-unsubscribes from multiple events. Removes all handlers for each event.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
client.unsubscribeMany({ events: ['event-a', 'event-b'] });
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
#### `connect(): void`
|
|
298
|
+
|
|
299
|
+
Manually connects the client socket. Useful after a manual `disconnect()`.
|
|
300
|
+
|
|
301
|
+
Returns early if the client instance does not exist.
|
|
302
|
+
|
|
303
|
+
#### `disconnect(): void`
|
|
304
|
+
|
|
305
|
+
Manually disconnects the client socket without cleaning up listeners or resetting state.
|
|
306
|
+
|
|
307
|
+
Returns early if the client instance does not exist.
|
|
308
|
+
|
|
309
|
+
> [!TIP]
|
|
310
|
+
> Use `shutdown()` instead of `disconnect()` for a full cleanup that also removes listeners and resets the authentication state.
|
|
311
|
+
|
|
312
|
+
#### `emit<T>(opts: { topic: string; data: T; doLog?: boolean; cb?: () => void }): void`
|
|
313
|
+
|
|
314
|
+
Emits an event to the server.
|
|
315
|
+
|
|
316
|
+
| Parameter | Type | Default | Description |
|
|
317
|
+
|-----------|------|---------|-------------|
|
|
318
|
+
| `topic` | `string` | -- | Event name |
|
|
319
|
+
| `data` | `T` | -- | Event payload |
|
|
320
|
+
| `doLog` | `boolean` | `false` | If `true`, logs the emission details |
|
|
321
|
+
| `cb` | `() => void` | `undefined` | Callback invoked asynchronously via `setImmediate` |
|
|
322
|
+
|
|
323
|
+
**Throws:**
|
|
324
|
+
- `'Invalid socket client state to emit'` (status 400) -- if the client is not connected
|
|
325
|
+
- `'Topic is required to emit'` (status 400) -- if `topic` is empty/falsy
|
|
326
|
+
|
|
327
|
+
#### `joinRooms(opts: { rooms: string[] }): void`
|
|
328
|
+
|
|
329
|
+
Requests to join rooms by emitting a `'join'` event to the server with `{ rooms }`. The server validates the request through its `validateRoomFn`.
|
|
330
|
+
|
|
331
|
+
Logs a warning and returns early if the client is not connected.
|
|
332
|
+
|
|
333
|
+
#### `leaveRooms(opts: { rooms: string[] }): void`
|
|
334
|
+
|
|
335
|
+
Requests to leave rooms by emitting a `'leave'` event to the server with `{ rooms }`.
|
|
336
|
+
|
|
337
|
+
Logs a warning and returns early if the client is not connected.
|
|
338
|
+
|
|
339
|
+
#### `shutdown(): void`
|
|
340
|
+
|
|
341
|
+
Fully shuts down the client:
|
|
342
|
+
|
|
343
|
+
1. Removes all event listeners (`removeAllListeners()`)
|
|
344
|
+
2. Disconnects if currently connected
|
|
345
|
+
3. Resets state to `UNAUTHORIZED`
|
|
346
|
+
|
|
347
|
+
## Types Reference
|
|
348
|
+
|
|
349
|
+
### `IHandshake`
|
|
350
|
+
|
|
351
|
+
Represents the client handshake data available during authentication.
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
interface IHandshake {
|
|
355
|
+
headers: IncomingHttpHeaders; // HTTP headers from the initial request
|
|
356
|
+
time: string; // Connection time as ISO string
|
|
357
|
+
address: string; // Client IP address
|
|
358
|
+
xdomain: boolean; // Whether the connection is cross-domain
|
|
359
|
+
secure: boolean; // Whether the connection uses TLS
|
|
360
|
+
issued: number; // Timestamp when the handshake was issued
|
|
361
|
+
url: string; // Request URL
|
|
362
|
+
query: ParsedUrlQuery; // Parsed query string parameters
|
|
363
|
+
auth: { [key: string]: any }; // Authentication payload sent by the client
|
|
364
|
+
}
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### `ISocketIOClient`
|
|
368
|
+
|
|
369
|
+
Internal representation of a connected client tracked by the server.
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
interface ISocketIOClient {
|
|
373
|
+
id: string; // Socket ID
|
|
374
|
+
socket: IOSocket; // The socket.io Socket instance
|
|
375
|
+
state: TSocketIOClientState; // 'unauthorized' | 'authenticating' | 'authenticated'
|
|
376
|
+
interval?: NodeJS.Timeout; // Ping interval timer (set after authentication)
|
|
377
|
+
authenticateTimeout: NodeJS.Timeout; // Auth timeout timer (cleared after authentication)
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### `TSocketIOClientState`
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
type TSocketIOClientState = 'unauthorized' | 'authenticating' | 'authenticated';
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### `TSocketIOAuthenticateFn`
|
|
388
|
+
|
|
389
|
+
Server-side authentication callback. Receives the handshake data and returns a boolean indicating whether to accept or reject the connection.
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
type TSocketIOAuthenticateFn = (args: IHandshake) => ValueOrPromise<boolean>;
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### `TSocketIOValidateRoomFn`
|
|
396
|
+
|
|
397
|
+
Server-side room validation callback. Receives the socket and requested rooms, returns the subset of rooms the client is allowed to join.
|
|
398
|
+
|
|
399
|
+
```typescript
|
|
400
|
+
type TSocketIOValidateRoomFn = (opts: {
|
|
401
|
+
socket: IOSocket;
|
|
402
|
+
rooms: string[];
|
|
403
|
+
}) => ValueOrPromise<string[]>;
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### `TSocketIOClientConnectedFn`
|
|
407
|
+
|
|
408
|
+
Server-side callback invoked after a client is fully authenticated and has joined default rooms.
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
type TSocketIOClientConnectedFn = (opts: {
|
|
412
|
+
socket: IOSocket;
|
|
413
|
+
}) => ValueOrPromise<void>;
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### `TSocketIOEventHandler<T>`
|
|
417
|
+
|
|
418
|
+
Client-side event handler type.
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
type TSocketIOEventHandler<T = unknown> = (data: T) => ValueOrPromise<void>;
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
### `IOptions`
|
|
425
|
+
|
|
426
|
+
Client connection options. Extends `SocketOptions` from `socket.io-client`.
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
interface IOptions extends SocketOptions {
|
|
430
|
+
path: string;
|
|
431
|
+
extraHeaders: Record<string | symbol | number, any>;
|
|
432
|
+
}
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### `TSocketIOServerOptions`
|
|
436
|
+
|
|
437
|
+
Discriminated union for server constructor options:
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
type TSocketIOServerOptions = ISocketIOServerNodeOptions | ISocketIOServerBunOptions;
|
|
441
|
+
|
|
442
|
+
interface ISocketIOServerNodeOptions extends ISocketIOServerBaseOptions {
|
|
443
|
+
runtime: 'node';
|
|
444
|
+
server: HTTPServer;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
interface ISocketIOServerBunOptions extends ISocketIOServerBaseOptions {
|
|
448
|
+
runtime: 'bun';
|
|
449
|
+
engine: any;
|
|
450
|
+
}
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
## Constants
|
|
454
|
+
|
|
455
|
+
### `SocketIOConstants`
|
|
456
|
+
|
|
457
|
+
| Constant | Value | Description |
|
|
458
|
+
|----------|-------|-------------|
|
|
459
|
+
| `EVENT_PING` | `'ping'` | Heartbeat event emitted by the server at `pingInterval` |
|
|
460
|
+
| `EVENT_CONNECT` | `'connection'` | Server-side connection event |
|
|
461
|
+
| `EVENT_DISCONNECT` | `'disconnect'` | Disconnect event (both server and client) |
|
|
462
|
+
| `EVENT_JOIN` | `'join'` | Room join request event |
|
|
463
|
+
| `EVENT_LEAVE` | `'leave'` | Room leave request event |
|
|
464
|
+
| `EVENT_AUTHENTICATE` | `'authenticate'` | Client-to-server authentication request |
|
|
465
|
+
| `EVENT_AUTHENTICATED` | `'authenticated'` | Server-to-client authentication success |
|
|
466
|
+
| `EVENT_UNAUTHENTICATE` | `'unauthenticated'` | Server-to-client authentication failure |
|
|
467
|
+
| `ROOM_DEFAULT` | `'io-default'` | Default room name |
|
|
468
|
+
| `ROOM_NOTIFICATION` | `'io-notification'` | Default notification room name |
|
|
469
|
+
|
|
470
|
+
### `SocketIOClientStates`
|
|
471
|
+
|
|
472
|
+
| Constant | Value | Description |
|
|
473
|
+
|----------|-------|-------------|
|
|
474
|
+
| `UNAUTHORIZED` | `'unauthorized'` | Initial state; not yet authenticated |
|
|
475
|
+
| `AUTHENTICATING` | `'authenticating'` | Authentication in progress |
|
|
476
|
+
| `AUTHENTICATED` | `'authenticated'` | Successfully authenticated |
|
|
477
|
+
|
|
478
|
+
#### `SocketIOClientStates.isValid(input: string): boolean`
|
|
479
|
+
|
|
480
|
+
Static method that checks whether a string is a valid client state value.
|
|
481
|
+
|
|
482
|
+
```typescript
|
|
483
|
+
SocketIOClientStates.isValid('authenticated'); // true
|
|
484
|
+
SocketIOClientStates.isValid('invalid'); // false
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Internal Defaults
|
|
488
|
+
|
|
489
|
+
| Constant | Value | Description |
|
|
490
|
+
|----------|-------|-------------|
|
|
491
|
+
| `CLIENT_AUTHENTICATE_TIMEOUT` | `10000` (10 s) | Default timeout before disconnecting unauthenticated clients |
|
|
492
|
+
| `CLIENT_PING_INTERVAL` | `30000` (30 s) | Default interval between heartbeat pings |
|
|
493
|
+
|
|
494
|
+
## See Also
|
|
495
|
+
|
|
496
|
+
- [Setup & Usage](./) -- Getting started, constructor options, and examples
|
|
497
|
+
- [WebSocket Helper](../websocket/) -- Bun-native WebSocket alternative
|