展示 HN:一个用于边缘应用的、可扩展的发布/订阅消息服务器
Show HN: An extensible pub/sub messaging server for edge applications

原始链接: https://github.com/narwhal-io/narwhal

## 独角鲸:适用于边缘应用的现代发布/订阅服务器 独角鲸是一个轻量级、可扩展的消息服务器,旨在简化聊天、物联网和移动应用等边缘应用的实时通信。它通过将核心消息层与特定于应用程序的逻辑分离,解决了现有方案的缺点——XMPP的复杂性和MQTT的僵化性。 这通过“调制器”实现,调制器是一个外部服务,处理身份验证、授权、验证,甚至业务逻辑。独角鲸专注于高效的消息路由,而调制器则提供系统的“大脑”,确保一致的应用程序行为。 独角鲸使用Rust构建,具有高性能和安全性,支持TLS/SSL、基于通道的访问控制,并通过TOML提供灵活的配置。它目前支持客户端到服务器、服务器到调制器以及调制器到服务器的连接。 目前处于Alpha阶段(v0.4.0),独角鲸正在积极开发中,计划的功能包括消息持久化、增强的可观察性和联合支持。 社区贡献和反馈受到高度鼓励。 [https://github.com/narwhal-io/narwhal](https://github.com/narwhal-io/narwhal)

## 独角鲸:一款用于边缘应用的定制化发布/订阅服务器 开发者 ortuman 发布了 Narwhal,一款用 Rust 构建的新型轻量级发布/订阅消息服务器,并寻求 Hacker News 社区的反馈。Narwhal 专为边缘应用(如在树莓派上运行的应用)设计,优先考虑定制化和可扩展性——允许开发者轻松修改路由和消息处理。 与 NATS 或 MQTT 等现有解决方案不同,Narwhal 旨在避免严格的默认设置,为特定用例提供更大的灵活性。选择 Rust 是出于对低内存占用和避免垃圾回收暂停的需求,这对于资源受限的边缘设备至关重要。 目前处于 Alpha 阶段,Narwhal 支持基本的发布/订阅功能,并计划添加持久性支持以实现可靠的消息传递。该项目在 GitHub 上开源,ortuman 欢迎贡献和改进建议。
相关文章

原文

Narwhal: An extensible pub/sub messaging server for edge applications

Narwhal was born out of a specific frustration: Building a modern chat feature shouldn't be this hard.

When trying to integrate real-time chat into a startup project, the existing options forced a difficult trade-off:

  1. XMPP (e.g., ejabberd): Powerful, but massive complexity, XML overhead, and a steep learning curve for simple needs.

  2. MQTT Brokers: Lightweight and fast, but rigid. Implementing custom authentication, dynamic permissions, or message validation often requires writing complex broker plugins in C/C++ or wrapping the broker in a "sidecar" mess.

Narwhal is the middle ground. It provides the lightweight performance of an edge broker but delegates the "brains" (Authentication, Authorization, Validation) to your application code via a Modulator.

A modulator is an external service that implements custom application logic on top of Narwhal's messaging layer. Rather than embedding application-specific features in the server, Narwhal delegates these concerns to a modulator, keeping the core server lightweight and focused on message routing.

Each Narwhal server connects to exactly one modulator, ensuring consistent application protocol semantics.

Common Modulator Use Cases:

  • Custom Authentication: JWT validation, OAuth flows, or proprietary auth schemes
  • Authorization & Access Control: Complex permission rules beyond basic channel ACLs
  • Content Validation: Message schemas, size limits, or content policies
  • Message Transformation: Encryption, compression, or message enrichment
  • Business Logic: Game logic, chat moderation, presence systems
  • Integration: Bridge with external services, databases, or APIs
demo.mp4
  • Designed for Edge Applications: Built specifically for mobile, desktop, or IoT environments
  • Modular Architecture: Extend the server with custom application logic via an external modulator
  • Secure by Default: TLS/SSL support with automatic certificate generation for development
  • Channel Management: Fine-grained access control and configuration per channel
  • High Performance: Asynchronous Rust implementation with efficient message routing (see benchmark results)
  • Rust 1.90 or later
  • OpenSSL
git clone https://github.com/narwhal-io/narwhal.git
cd narwhal
cargo build --release

The compiled binary will be available at target/release/narwhal.

# Run with default configuration
cargo run --bin narwhal

# Or with a custom config file
cargo run --bin narwhal -- --config path/to/config.toml

Once the server is running, you can test the connection using OpenSSL:

openssl s_client -connect 127.0.0.1:22622 -ign_eof

Narwhal supports three connection types:

  1. Client-to-Server (C2S): End-user clients connecting to the Narwhal server
  2. Server-to-Modulator (S2M): Server-initiated connection to the modulator for delegating operations
  3. Modulator-to-Server (M2S): Modulator-initiated connection for sending private messages to clients
graph LR
    Clients["Clients"] <-->|C2S| Server["Narwhal Server"]
    Server <-->|S2M/M2S| Modulator["Modulator"]
Loading

Narwhal uses TOML format for configuration. See the examples/config/ directory for examples.

The repository includes several example modulators in the examples/modulator/ directory:

  • plain-authenticator: Simple username/password authentication
  • broadcast-payload-json-validator: Validates JSON message payloads
  • broadcast-payload-csv-validator: Validates CSV message payloads
  • private-payload-sender: Demonstrates sending private messages to clients

Each example demonstrates different aspects of building modulators for Narwhal.

narwhal/
├── crates/
│   ├── benchmark/       # Performance benchmarking tools
│   ├── client/          # Client libraries
│   ├── common/          # Shared types and utilities
│   ├── modulator/       # Modulator client/server implementation
│   ├── protocol/        # Protocol message definitions
│   ├── protocol-macros/ # Protocol code generation macros
│   ├── server/          # Main Narwhal server
│   ├── test-util/       # Testing utilities
│   └── util/            # General utilities
├── docs/                # Documentation
├── examples/            # Example configurations and modulators
└── README.md

Narwhal includes a benchmark tool to measure throughput and latency performance:

# Build the benchmark tool
cargo build --bin narwhal-bench --release

# Run a basic benchmark against a local server
./target/release/narwhal-bench \
  --server 127.0.0.1:22622 \
  --producers 1 \
  --consumers 1 \
  --duration 1m \
  --max-payload-size 256

The benchmark tool simulates multiple producer and consumer clients connecting to a Narwhal server and exchanging messages. It reports metrics such as:

  • Message throughput (messages/second)
  • Latency percentiles (p50, p90, p99)
  • Connection success rates
  • Total messages sent and received

Running with Debug Tracing

RUST_LOG=debug cargo run --bin narwhal

Current Version: 0.4.0 (Alpha)

Narwhal is in active development and currently in alpha stage. While the core functionality is working and tested, please note:

  • APIs may change before reaching 1.0.0 - Breaking changes may occur as we refine the protocol and interfaces based on community feedback
  • Evaluation and development use - Suitable for testing, proof-of-concepts, and non-production environments
  • Community feedback welcome - We're actively seeking input to improve Narwhal before stabilizing the 1.0.0 API

If you're interested in using Narwhal in production, we encourage you to get involved, provide feedback, and help shape the future of the project!

We're actively working on expanding Narwhal's capabilities. Here are some features planned for future releases:

  • Message Persistence: Durable message storage for reliable message delivery
  • Enhanced Observability: Built-in metrics, tracing, and monitoring capabilities
  • Performance Optimizations: Continued improvements to throughput and latency
  • Additional Protocol Transports: Support for WebSocket and other transport layers
  • Federation Support: Enable multiple Narwhal servers to communicate and share messages across distributed deployments, allowing for horizontal scaling and multi-region architectures

We welcome contributions! Please see our Contributing Guide for details on:

  • Reporting bugs
  • Suggesting features
  • Submitting pull requests
  • Development setup

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

联系我们 contact @ memedata.com