Getting started
Install#
npm install effect@~4.0.6 effect-rpc-pipelineFor the Node.js quickstart, also install the platform package and a TypeScript runner:
npm install @effect/platform-node@4.0.6
npm install --save-dev tsx
npm pkg set type=moduleQuickstart#
This example finds a user, then fetches their profile in the same pipeline. Create the following three files in one directory.
1. Define the RPC contract#
Create rpcs.ts. Both the server and client import this group.
import * as Schema from "effect/Schema"
import * as Rpc from "effect/rpc/Rpc"
import * as RpcGroup from "effect/rpc/RpcGroup"
export const AppRpcs = RpcGroup.make(
Rpc.make("findUser", {
payload: { email: Schema.String },
success: Schema.Struct({ id: Schema.String, name: Schema.String })
}),
Rpc.make("getProfile", {
payload: { userId: Schema.String },
success: Schema.Struct({ bio: Schema.String })
})
)2. Implement and serve the RPCs#
Create server.ts. The handlers below return sample data; an application would read from its own services.
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node"
import { createServer } from "node:http"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import * as HttpRouter from "effect/http/HttpRouter"
import * as RpcSerialization from "effect/rpc/RpcSerialization"
import * as PipelineServer from "effect-rpc-pipeline/PipelineServer"
import { AppRpcs } from "./rpcs.js"
const Handlers = AppRpcs.toLayer({
findUser: () => Effect.succeed({ id: "ada", name: "Ada" }),
getProfile: ({ userId }) => Effect.succeed({ bio: `Profile for ${userId}` })
})
const Routes = PipelineServer.layerHttp({ group: AppRpcs, path: "/rpc" }).pipe(
Layer.provide(Handlers)
)
const Server = HttpRouter.serve(Routes).pipe(
Layer.provide(NodeHttpServer.layer(createServer, { port: 8787, host: "127.0.0.1" })),
Layer.provide(RpcSerialization.layerJson)
)
Layer.launch(Server).pipe(NodeRuntime.runMain)3. Compose the client calls#
Create client.ts. user.id is a reference to the server’s future result, so it can be passed directly to getProfile.
import * as Effect from "effect/Effect"
import * as RpcSerialization from "effect/rpc/RpcSerialization"
import * as Pipeline from "effect-rpc-pipeline/Pipeline"
import * as PipelineClient from "effect-rpc-pipeline/PipelineClient"
import { AppRpcs } from "./rpcs.js"
const program = Effect.gen(function*() {
const rpc = yield* PipelineClient.makeHttp(AppRpcs, {
url: "http://127.0.0.1:8787/rpc"
})
const user = rpc.findUser({ email: "ada@example.com" })
return yield* Pipeline.all({
user,
profile: rpc.getProfile({ userId: user.id })
})
}).pipe(Effect.provide(RpcSerialization.layerJson))
console.log(await Effect.runPromise(program))4. Run the example#
Start the server, then run the client in a second terminal:
node --import tsx server.ts
node --import tsx client.tsThe client prints:
{ user: { id: "ada", name: "Ada" }, profile: { bio: "Profile for ada" } }The server ran findUser first, then passed its ID to getProfile. Both calls traveled in one HTTP pipeline request.
The quickstart source files are checked by TypeScript. To run them from this repository, build the package first, then use node --import tsx docs/snippets/server.ts and node --import tsx docs/snippets/client.ts.