@flexent/protocomm

中文文档
2.22.1发布于 2026/11/13MITES Module近 30 天下载 2434

Bi-directional transport-agnostic JSON-based messaging

原始使用文档
文档目录5 节
  1. Highlights
  2. Introduction
  3. Concepts
  4. Usage
  5. WebSocket Client

Protocomm

Bi-directional transport-agnostic JSON-based messaging.

Status: experimental, subject to change.

Highlights

  • 🔥 Transport-agnostic
  • 💻 Fully typed domains, methods, and events
  • 🗜 Validation based on airtight schemas

Introduction

In software engineering communication protocols are defined as formal rules for transmitting the information. Modern systems tend to deal with application-level protocols such as HTTP which are already considered high level in the OSI stack. However, in practice the need often arises for implementing even more application- or domain-specific protocols on top of the existing ones.

One widely known example would be a typical RESTful API which is in fact a protocol between the application server providing the functionality and the frontend application using such functionality.

Protocols aren't cheap. Good protocols try to address a lot of concerns and have to deal with a fair amounts of trade-offs. This drives a lot of initiatives to further standardize the application protocols written on top of other application protocols (e.g. Swagger/OpenAPI, JSON-RPC, gRPC-over-HTTP, etc). Such initiatives oftentimes have different goals, limitations and implications.

This library is one such initiative with following key considerations:

  • It is designed for modern web applications written in TypeScript.
  • Such applications may consist of many different components "talking" to each other.
  • Such components may or may not exist in the same runtime/process/node. They may or may not have networking layer to communicate over.
  • Some example of communications:
    • frontend application talking to a backend via HTTP (i.e. your typical webapp)
    • frontend application sending requests to a backend via WebSocket and receiving the responses and events asynchronously
    • a web page sending and receiving messages to/from the isolated iframe
    • multiple processes communicating with each other using IPC
    • two different systems talking to each other via TCP
  • Protocols need to be transport-agnostic. The transport is abstracted as a readable/writeable streams. Protocomm will take care of serializing and deserializing the payloads.
  • Protocols should be fully typesafe on both sides of the communication. We rely on TypeScript to provide both Client and Handler with compile-time type safety guarantees.
  • Protocol messages and payload need to be validated in runtime. Runtime validation should fully correspond to compile-time validation.
  • Protocol messages should have a reasonable strategy for dealing with protocol evolution.
  • It should be possible to generate the documentation from the protocol definitions.

Protocomm is loosely based on JSON-RPC and is inspired by Chrome DevTools Protocol and Protobuf.

Concepts

  • Communication always happen between a Client and a Handler.

    Their roles in a particular communication are not equal: client is always an initiator, and the handler is typically expected to handle multiple clients (although this is not enforced in any way).

  • Protocol is an interface that both Client and Handler need to adhere to. It consists of one or more domains.

  • Domain is an interface that defines methods and events.

  • Each domain needs a corresponding Domain Definition — a static JSON object with metadata available in runtime. It describes the schema of methods and events and is used to automatically generate the client, validate the payloads, as well as for other meta purposes (such as documentation).

  • Protocol Index is a collection of Domain Definition objects, corresponding to a particular protocol.

A protocol is typically implemented as a standalone isomorphic package shared between both parties and contains type definitions, schema and domain definitions of all entities and DTOs used in communication. Such package is then used on the "backend" side to implement the necessary functionality and on the "consumer" side to generate the client.

Usage

Please refer to the tests setup for an example of fictional service.

WebSocket Client

Use WsClient from @flexent/protocomm for a typed WebSocket transport.

import { ProtocolIndex, WsClient } from '@flexent/protocomm';

class ApiWsClient extends WsClient<MyProtocol> {
    protected get url() {
        return 'ws://127.0.0.1:8080/ws';
    }
    protected get protocol() {
        return new ProtocolIndex(myProtocol);
    }
    protected override shouldReconnectOnClose() {
        return true;
    }
}

const wsClient = new ApiWsClient();
await wsClient.connect();
await wsClient.client.system.ping({});

WsClient supports:

  • protocol-typed client generated by RpcClient
  • mandatory protocol and URL via abstract getters
  • overridable defaults for retry/reconnect and socket creation

版本与发布记录

@flexent/protocomm 共发布 51 个版本,首次发布于 2022/09/13;过去 12 个月发布了 40 个版本。

版本发布日期距今
2.22.1latest2026/11/132 天前
2.22.02026/11/132 天前
2.21.02026/11/132 天前
2.20.22026/11/133 天前
2.20.12026/11/115 天前
2.20.02026/11/069 天前

关于 @flexent/protocomm 的常见问题

@flexent/protocomm 是什么?

Bi-directional transport-agnostic JSON-based messaging

如何安装 @flexent/protocomm?

在项目目录运行 npm install @flexent/protocomm,或使用 pnpm add / yarn add / bun add。

@flexent/protocomm 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flexent__protocomm 是否存在。

@flexent/protocomm 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flexent/protocomm 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flexent/protocomm 可以商用吗?

@flexent/protocomm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flexent/protocomm 的下载量有多少?

根据 npm 官方下载统计,@flexent/protocomm 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,434 次,日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flexent/protocomm 还在维护吗?

最新版本 2.22.1 发布于 2026/11/13(2 天前);至今共发布 51 个版本,其中过去 12 个月发布了 40 个;首次发布于 2022/09/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。