Skip to content

bug-ops/pjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

63 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

PJS - Priority JSON Streaming Protocol

Crates.io Documentation Rust Build WASM Build codecov License

๐Ÿš€ Priority-based streaming | ๐ŸŽฏ Progressive loading | ๐Ÿ’พ Zero-copy operations | ๐ŸŒ WebAssembly ready

High-performance Rust library for priority-based JSON streaming with SIMD acceleration. Stream large JSON responses progressively, delivering critical data first while background data loads asynchronously.

v0.3.0: WebAssembly support, Clean Architecture compliance, 500+ tests passing, zero clippy warnings. Requires nightly Rust for zero-cost GAT abstractions.

Features

  • โšก Blazing Fast - SIMD-accelerated parsing with optimized performance
  • ๐ŸŽฏ Smart Streaming - Priority-based delivery sends critical data first, skeleton-first rendering
  • ๐Ÿ’พ Memory Efficient - Optimized progressive loading, bounded memory usage, zero-copy operations
  • ๐ŸŒ WebAssembly - Browser and Node.js support with compact bundle size
  • ๐Ÿ“Š Schema Aware - Automatic compression and semantic analysis
  • ๐Ÿ”ง Production Ready - Clean Architecture, comprehensive tests, Prometheus metrics

Performance

Benchmark Performance Gain Notes
Small JSON Competitive Comparable to industry standards
Medium JSON ~3x faster vs traditional parsers
Large JSON ~6x faster vs traditional parsers
Progressive Loading ~5x faster vs batch processing

Installation

cargo add pjson-rs

Or add to Cargo.toml:

[dependencies]
pjson-rs = "0.3.0"

Quick Start

Rust HTTP Server

use pjson_rs::infrastructure::http::axum_adapter::create_pjs_router;

#[tokio::main]
async fn main() {
    let app = create_pjs_router().with_state(app_state);
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
    axum::serve(listener, app).await?;
}

Start server and test:

# Run example server
cargo run --example axum_server

# Create session
curl -X POST http://localhost:3000/pjs/sessions \
  -H "Content-Type: application/json" \
  -d '{"max_concurrent_streams": 5}'

# Stream data with priority
curl http://localhost:3000/pjs/stream/{session_id}/sse

WebAssembly (Browser)

npm install pjs-wasm
<script type="module">
import init, { PjsParser, PriorityConstants } from './pkg/pjs_wasm.js';

async function main() {
    await init();
    const parser = new PjsParser();

    const frames = parser.generateFrames(
        JSON.stringify({ user_id: 123, name: "Alice" }),
        PriorityConstants.MEDIUM
    );

    frames.forEach(frame => {
        if (frame.priority >= 90) {
            updateUI(frame.data); // Critical data first
        }
    });
}

main();
</script>

WebAssembly (Node.js)

import init, { PjsParser } from 'pjs-wasm';
import { readFile } from 'fs/promises';

const wasmBuffer = await readFile('./node_modules/pjs-wasm/pkg/pjs_wasm_bg.wasm');
await init(wasmBuffer);

const parser = new PjsParser();
const frames = parser.generateFrames(JSON.stringify(data), 50);

frames.forEach(frame => {
    console.log(`Priority ${frame.priority}: ${frame.frame_type}`);
});

Build WASM from Source

# Install wasm-pack
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# Build for different targets
wasm-pack build crates/pjs-wasm --target web --release      # Browsers
wasm-pack build crates/pjs-wasm --target nodejs --release   # Node.js
wasm-pack build crates/pjs-wasm --target bundler --release  # Webpack/Rollup

Use Cases

  • ๐Ÿ“Š Real-time Dashboards - Show key metrics instantly, load details progressively
  • ๐Ÿ“ฑ Mobile Apps - Optimize for slow networks, critical data first
  • ๐Ÿ›๏ธ E-commerce - Product essentials load immediately, reviews/images follow
  • ๐Ÿ“ˆ Financial Platforms - Trading data prioritized over historical charts
  • ๐ŸŽฎ Gaming Leaderboards - Player rank appears instantly, full list streams in

Building

Prerequisites

# Requires nightly Rust for GAT features
rustup install nightly
rustup override set nightly

Build Commands

# Standard build
cargo build --release

# Run tests
cargo test --workspace

# Run benchmarks
cargo bench -p pjs-bench

# Run demo servers
cargo run --bin websocket_streaming --manifest-path crates/pjs-demo/Cargo.toml
cargo run --bin interactive_demo --manifest-path crates/pjs-demo/Cargo.toml

Feature Flags

# SIMD optimization
simd-auto      # Auto-detect (default)
simd-avx2      # AVX2 support
simd-neon      # ARM NEON

# Memory allocators (optional)
jemalloc       # tikv-jemallocator
mimalloc       # mimalloc

# Features
schema-validation     # Schema validation (default)
compression           # Schema-based compression
http-server           # Axum HTTP server
websocket-server      # WebSocket streaming
prometheus-metrics    # Prometheus integration

Architecture

PJS follows Clean Architecture with Domain-Driven Design:

  • pjs-domain - Pure business logic, WASM-compatible (43 tests)
  • pjs-wasm - WebAssembly bindings for browsers & Node.js (25 tests)
  • pjs-core - Rust implementation with HTTP/WebSocket integration (400+ tests)
  • pjs-demo - Interactive demo servers with real-time streaming
  • pjs-js-client - TypeScript/JavaScript client library
  • pjs-bench - Comprehensive performance benchmarks

Implementation: โœ… Complete (500+ tests, zero clippy warnings)

Contributing

Contributions welcome! Please ensure:

rustup override set nightly
cargo clippy --workspace -- -D warnings
cargo test --workspace --all-features
cargo fmt --check

See CONTRIBUTING.md for guidelines.

License

Licensed under either of:

at your option.

Resources


PJS: Priority-based JSON streaming for instant user experiences.

About

Priority JSON Streaming Protocol

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •