# @zerva/rpc

> 🌱 Zerva websocket rpc module

Latest version **0.90.0** (published 2026-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zerva/rpc
pnpm add @zerva/rpc
yarn add @zerva/rpc
bun add @zerva/rpc
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.90.0 |
| Published | 2026-03-30 |
| First published | 2024-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.0.0 |
| Dependencies | 4 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dirk Holtwick |
| Maintainers | zerva |
| Keywords | server, typescript, event, rpc, websocket, socket, zerva, zeed |

## Links

- npm: https://www.npmjs.com/package/@zerva/rpc
- Funding: https://github.com/sponsors/holtwick
- npm.io page: https://npm.io/package/@zerva/rpc

## Dependencies (4)

- [zeed](https://npm.io/package/zeed.md) ^1.3.1
- [@zerva/core](https://npm.io/package/@zerva/core.md) 0.90.0
- [@zerva/http](https://npm.io/package/@zerva/http.md) 0.90.0
- [@zerva/websocket](https://npm.io/package/@zerva/websocket.md) 0.90.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.90.0 (latest) — 2026-03-30
- 0.88.0 — 2026-03-30
- 0.86.1 — 2026-03-13
- 0.81.1 — 2025-10-22
- 0.80.1 — 2025-10-13
- 0.79.0 — 2025-10-10
- 0.78.1 — 2025-10-09
- 0.77.1 — 2025-10-08
- 0.74.4 — 2025-09-19
- 0.74.2 — 2025-09-10
- 0.74.1 — 2025-09-04
- 0.73.1 — 2025-08-31
- 0.73.0 — 2025-08-31
- 0.72.0 — 2025-08-24
- 0.71.0 — 2025-06-14
- … 32 more at https://npm.io/package/@zerva/rpc/versions

## README

# 🌱 Zerva useWebSocketRpcHub

**This is a side project of [Zerva](https://github.com/holtwick/zerva)**

A reliable way to realize function calls between client and server via web sockets. It is taken care of the infrastucture so you can focus on the functionality.

First step should be to create a shared definition of functions on the server side and the client side. Example:

```ts
// functions.ts
export interface RpcClientFunctions {
  updateCounter: (count: number) => void
}

export interface RpcServerFunctions {
  helloIAmNew: (id: string) => number
}
```

On the server side initialize the zerva module and then listen for `rpcConnect` like this:

```ts
// server.ts

useWebsocketRpcHub()

let clientCounter = 1
on('rpcConnect', async ({ rpcHub, dispose }) => {
  const rpc = rpcHub<RpcServerFunctions, RpcClientFunctions>({
    helloIAmNew(id) {
      log.info('New client with user agent:', id)
      return clientCounter++
    },
  })

  let counter = 0

  dispose.interval(() => {
    rpc.updateCounter(counter++)
  }, 1000)
})
```

On the client side almost the same but vice-versa:

```ts
// client.ts

import { useWebsocketRpcHubClient } from '@zerva/rpc'
import { ref } from 'vue'
import type { RpcClientFunctions, RpcServerFunctions } from './_types'

const { rpcHub } = useWebsocketRpcHubClient()

export const counter = ref(-1)

export const rpc = rpcHub<RpcClientFunctions, RpcServerFunctions>({
  updateCounter(count: number) {
    counter.value = count
  },
})
```

On both ends use your custom `rpc` to call functions as defined, await results if desired.

This RPC implementation is designed as a "hub" i.e. you can use the same connection to create multiple `rpc` definitions and implementations. That becomes handy if you are doing plugin like development.

---
_Source: https://npm.io/package/@zerva/rpc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
