@@ -43,10 +43,10 @@ yarn add zod-sockets zod socket.io typescript
43
43
## Set up config
44
44
45
45
``` typescript
46
- import { createConfig } from " zod-sockets" ;
46
+ import { createSimpleConfig } from " zod-sockets" ;
47
47
48
- // defaults: root namespace only, console logger, timeout 2s
49
- const config = createConfig ();
48
+ // shorthand for root namespace only, defaults: console logger, timeout 2s
49
+ const config = createSimpleConfig ();
50
50
```
51
51
52
52
## Create a factory
@@ -111,36 +111,6 @@ for sending the `ping` event to `ws://localhost:8090` with acknowledgement.
111
111
112
112
# Basic features
113
113
114
- ## Namespaces first
115
-
116
- Namespaces allow you to separate incoming and outgoing events into groups, in which events can have the same name, but
117
- different essence, payload and handlers. You can add ` namespaces ` to the argument of ` createConfig() ` or use
118
- ` addNamespace() ` method after it. The default namespace is a root one having ` path ` equal to ` / ` . Namespaces may have
119
- ` emission ` and ` hooks ` .
120
- Read the Socket.IO [ documentation on namespaces] ( https://socket.io/docs/v4/namespaces/ ) .
121
-
122
- ``` typescript
123
- import { createConfig } from " zod-sockets" ;
124
-
125
- const config = createConfig ({
126
- namespaces: {
127
- // The namespace "/public"
128
- public: {
129
- emission: { chat: { schema } },
130
- hooks: {
131
- onStartup : () => {},
132
- onConnection : () => {},
133
- onDisconnect : () => {},
134
- onAnyIncoming : () => {},
135
- onAnyOutgoing : () => {},
136
- },
137
- },
138
- },
139
- }).addNamespace ({
140
- path: " private" , // The namespace "/private" has no emission
141
- });
142
- ```
143
-
144
114
## Emission
145
115
146
116
The outgoing events should be configured using ` z.tuple() ` schemas. Those tuples describe the types of the arguments
@@ -151,10 +121,9 @@ development. Consider the following examples of two outgoing events, with and wi
151
121
152
122
``` typescript
153
123
import { z } from " zod" ;
154
- import { createConfig } from " zod-sockets" ;
124
+ import { createSimpleConfig } from " zod-sockets" ;
155
125
156
- const config = createConfig ().addNamespace ({
157
- // path: "/", // optional, default: root namespace
126
+ const config = createSimpleConfig ({
158
127
emission: {
159
128
// enabling Socket::emit("chat", "message", { from: "someone" })
160
129
chat: {
@@ -222,15 +191,15 @@ The library supports any logger having `info()`, `debug()`, `error()` and
222
191
223
192
``` typescript
224
193
import pino , { Logger } from " pino" ;
225
- import { createConfig } from " zod-sockets" ;
194
+ import { createSimpleConfig } from " zod-sockets" ;
226
195
227
196
const logger = pino ({
228
197
transport: {
229
198
target: " pino-pretty" ,
230
199
options: { colorize: true },
231
200
},
232
201
});
233
- const config = createConfig ({ logger });
202
+ const config = createSimpleConfig ({ logger });
234
203
235
204
// Setting the type of logger used
236
205
declare module " zod-sockets" {
@@ -241,16 +210,16 @@ declare module "zod-sockets" {
241
210
### With Express Zod API
242
211
243
212
If you're using ` express-zod-api ` , you can reuse the same logger. If it's a custom logger — supply the same instance to
244
- both ` createConfig() ` methods of two libraries. In case you're using the default ` winston ` logger provided by
213
+ configs of both libraries. In case you're using the default ` winston ` logger provided by
245
214
` express-zod-api ` , you can obtain its instance from the returns of the ` createServer() ` method.
246
215
247
216
``` typescript
248
217
import { createServer } from " express-zod-api" ;
249
- import { createConfig } from " zod-sockets" ;
218
+ import { createSimpleConfig } from " zod-sockets" ;
250
219
import type { Logger } from " winston" ;
251
220
252
221
const { logger } = await createServer ();
253
- const config = createConfig ({ logger });
222
+ const config = createSimpleConfig ({ logger });
254
223
255
224
// Setting the type of logger used
256
225
declare module " zod-sockets" {
@@ -342,9 +311,9 @@ emit events regardless the incoming ones by setting the `onConnection` property
342
311
argument, which has a similar interface except ` input ` and fires for every connected client:
343
312
344
313
``` typescript
345
- import { createConfig } from " zod-sockets" ;
314
+ import { createSimpleConfig } from " zod-sockets" ;
346
315
347
- const config = createConfig (). addNamespace ({
316
+ const config = createSimpleConfig ({
348
317
// emission: { ... },
349
318
hooks: {
350
319
onConnection : async ({ client , withRooms , all }) => {
@@ -360,9 +329,9 @@ Moreover, you can emit events regardless the client activity at all by setting t
360
329
of the ` addNamespace() ` argument. The implementation may have a ` setInterval() ` for recurring emission.
361
330
362
331
``` typescript
363
- import { createConfig } from " zod-sockets" ;
332
+ import { createSimpleConfig } from " zod-sockets" ;
364
333
365
- const config = createConfig (). addNamespace ({
334
+ const config = createSimpleConfig ({
366
335
hooks: {
367
336
onStartup : async ({ all , withRooms }) => {
368
337
// sending to everyone in a room
@@ -440,9 +409,9 @@ Please avoid transformations in those schemas since they are not going to be app
440
409
441
410
``` typescript
442
411
import { z } from " zod" ;
443
- import { createConfig } from " zod-sockets" ;
412
+ import { createSimpleConfig } from " zod-sockets" ;
444
413
445
- const config = createConfig (). addNamespace ({
414
+ const config = createSimpleConfig ({
446
415
metadata: z .object ({
447
416
/** @desc Number of messages sent to the chat */
448
417
msgCount: z .number ().int (),
@@ -475,6 +444,36 @@ const handler = async ({ client }) => {
475
444
};
476
445
```
477
446
447
+ ## Namespaces
448
+
449
+ Namespaces allow you to separate incoming and outgoing events into groups, in which events can have the same name, but
450
+ different essence, payload and handlers. For using namespaces replace the ` createSimpleConfig() ` method with
451
+ ` new Config() ` , then use its ` .addNamespace() ` method for each namespace. Namespaces may have ` emission ` and ` hooks ` .
452
+ Read the Socket.IO [ documentation on namespaces] ( https://socket.io/docs/v4/namespaces/ ) .
453
+
454
+ ``` typescript
455
+ import { Config } from " zod-sockets" ;
456
+
457
+ const config = new Config ({
458
+ logger ,
459
+ timeout: 2000 ,
460
+ })
461
+ .addNamespace ({
462
+ // The namespace "/public"
463
+ emission: { chat: { schema } },
464
+ hooks: {
465
+ onStartup : () => {},
466
+ onConnection : () => {},
467
+ onDisconnect : () => {},
468
+ onAnyIncoming : () => {},
469
+ onAnyOutgoing : () => {},
470
+ },
471
+ })
472
+ .addNamespace ({
473
+ path: " private" , // The namespace "/private" has no emission
474
+ });
475
+ ```
476
+
478
477
# Integration
479
478
480
479
## Exporting types for frontend
0 commit comments