Skip to content

HTTP, WebSocket & IPC server

@tevm/server (also exported as tevm/server) exposes any MemoryClient over HTTP, WebSocket subscriptions, Unix IPC sockets, Express middleware, or Next.js API routes. All transports share one native engine and forward raw JSON-RPC directly through client.transport.tevm.rpc().

createServer(client) creates a Node.js http.Server with WebSocket upgrade handling attached on the same port:

server.ts
import { createMemoryClient } from 'tevm'
import { createServer } from 'tevm/server'
const client = createMemoryClient()
await client.tevmReady()
const server = createServer(
client,
{},
{
cors: true,
maxBodySize: 1024 * 1024, // 1 MiB (default)
maxBatchSize: 100,
requestTimeout: 30_000, // 30s (default)
},
)
server.listen(8545, '127.0.0.1', () => {
console.log('Listening on http://127.0.0.1:8545 (HTTP + WebSocket)')
})
process.once('SIGINT', () => {
// Close the server first so new requests get rejected cleanly, then close the engine
server.close(() => {
void client.tevmClose()
})
})

Servers created with createServer or createWebSocketServer support per-connection subscriptions backed by native ZEVM filters:

  • newHeads — streams block headers as blocks are mined.
  • logs — streams matching event logs (accepts standard { address, topics } filters).
  • newPendingTransactions — streams pending transaction hashes.
  • syncing — emits sync status (false).

Disconnecting a WebSocket uninstalls all native filters owned by that connection automatically.

ws-client.ts
import { createPublicClient, webSocket } from 'viem'
// Works in Node.js and browsers
const publicClient = createPublicClient({
transport: webSocket('ws://127.0.0.1:8545'),
})
const unwatch = publicClient.watchBlockNumber({
onBlockNumber: (blockNumber) => console.log('New block:', blockNumber),
})

Use createIpcServer(client) for local tools that speak newline-delimited JSON-RPC over Unix domain sockets (or TCP):

ipc-server.ts
import { createMemoryClient } from 'tevm'
import { createIpcServer } from 'tevm/server'
const client = createMemoryClient()
await client.tevmReady()
const ipc = createIpcServer(client)
ipc.listen('/tmp/tevm.sock')

createIpcServer handles split frames and concatenated JSON objects in a single chunk, and rejects oversized frames without crashing the socket server.

Attach TEVM to an existing Node.js web server without opening a second port:

express-app.ts
import express from 'express'
import { createMemoryClient } from 'tevm'
import { createExpressMiddleware } from 'tevm/server'
const app = express()
const client = createMemoryClient()
app.use('/rpc', createExpressMiddleware(client))
app.listen(3000)

From another Node.js process or a browser app, connect to a running server with createHttpClient (from tevm/http-client or @tevm/http-client) to get viem’s public actions plus typed remote.tevm.* helpers:

remote-client.ts
import { createHttpClient } from 'tevm/http-client'
const remote = createHttpClient({ url: 'http://127.0.0.1:8545' })
const blockNumber = await remote.getBlockNumber()
const account = await remote.tevm.getAccount({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
})
console.log({ blockNumber, balance: account.balance })