@@ -281,88 +281,3 @@ export class UsersClient extends Effect.Service<UsersClient>()("UsersClient", {
281
281
dependencies: [AuthClientLive ]
282
282
}) {}
283
283
```
284
-
285
- ## Defining the Router
286
-
287
- In the router setup, we link the Counts request to a function that emits numbers from 1 to 5 at regular intervals.
288
-
289
- ``` ts filename="router.ts"
290
- // router.ts
291
- import { Router , Rpc } from " @effect/rpc"
292
- import { Effect , Stream } from " effect"
293
- import { Counts } from " ./request.js"
294
-
295
- export const appRouter = Router .make (
296
- Rpc .stream (Counts , () =>
297
- Stream .make (1 , 2 , 3 , 4 , 5 ).pipe (Stream .tap (() => Effect .sleep (" 1 second" )))
298
- )
299
- )
300
-
301
- export type AppRouter = typeof appRouter
302
- ` ` `
303
-
304
- ## Serving the API
305
-
306
- The server code configures an HTTP server to handle requests, using our appRouter to manage incoming stream requests.
307
-
308
- ` ` ` ts filename =" server.ts"
309
- // server.ts
310
- import { HttpRouter , HttpServer } from " @effect/platform"
311
- import { NodeHttpServer , NodeRuntime } from " @effect/platform-node"
312
- import { toHttpApp } from " @effect/rpc-http/HttpRpcRouter"
313
- import { Layer } from " effect"
314
- import { createServer } from " http"
315
- import { appRouter } from " ./router.js"
316
-
317
- const HttpLive = HttpRouter .empty .pipe (
318
- HttpRouter .post (" /rpc" , toHttpApp (appRouter )),
319
- HttpServer .serve (),
320
- HttpServer .withLogAddress ,
321
- Layer .provide (NodeHttpServer .layer (createServer , { port: 3000 }))
322
- )
323
-
324
- NodeRuntime .runMain (Layer .launch (HttpLive ))
325
- ```
326
-
327
- ## Consuming the Stream from the Client
328
-
329
- ``` ts filename="client.ts"
330
- // client.ts
331
- import { HttpClient , HttpClientRequest } from " @effect/platform"
332
- import { RpcResolver } from " @effect/rpc"
333
- import { HttpRpcResolver } from " @effect/rpc-http"
334
- import { Effect , Stream } from " effect"
335
- import { Counts } from " ./request.js"
336
- import type { AppRouter } from " ./router.js"
337
-
338
- const makeClient = Effect .gen (function * () {
339
- const baseClient = yield * HttpClient .HttpClient
340
- const client = baseClient .pipe (
341
- HttpClient .filterStatusOk ,
342
- HttpClient .mapRequest (
343
- HttpClientRequest .prependUrl (" http://localhost:3000/rpc" )
344
- )
345
- )
346
- return RpcResolver .toClient (HttpRpcResolver .make <AppRouter >(client ))
347
- })
348
-
349
- const program = Effect .gen (function * () {
350
- const client = yield * makeClient
351
- yield * Effect .log (" Running the client" )
352
- const stream = client (new Counts ())
353
- return yield * stream .pipe (
354
- Stream .tap ((element ) => Effect .log (element )),
355
- Stream .runCollect
356
- )
357
- })
358
-
359
- program .pipe (Effect .provide (FetchHttpClient .layer ), Effect .runPromise )
360
- /*
361
- timestamp=...:50.395Z level=INFO fiber=#0 message="Running the client"
362
- timestamp=...:52.438Z level=INFO fiber=#1 message=1
363
- timestamp=...:53.438Z level=INFO fiber=#1 message=2
364
- timestamp=...:54.440Z level=INFO fiber=#1 message=3
365
- timestamp=...:55.445Z level=INFO fiber=#1 message=4
366
- timestamp=...:55.447Z level=INFO fiber=#1 message=5
367
- */
368
- ```
0 commit comments