Skip to content

Conversation

@amitu
Copy link
Contributor

@amitu amitu commented Sep 17, 2025

Summary

Implement the foundational streaming API for fastn-p2p to enable remote shell functionality. This PR adds streaming sessions alongside the existing request/response pattern, providing the foundation for rshell/rexec commands.

🎯 Implementation: fastn-p2p Streaming API

This PR implements Phase 2 of the focused plan originally outlined in PR #2202:

  • Add client module with connect() function
  • Implement client::Session with stdin/stdout streams
  • Add basic open_uni()/accept_uni() for stderr-like streams
  • Add server::Session with protocol negotiation and context integration
  • Add Session -> Request conversion (into_request()) for RPC compatibility
  • Preserve existing Request methods and call() API unchanged
  • Basic context integration (sessions get context from fastn-context)

🔧 New Streaming API

Client Side

// Establish streaming session
let session = fastn_p2p::client::connect(our_key, target, protocol).await?;

// Main stdin/stdout streams
session.stdin   // Send to server
session.stdout  // Receive from server

// Additional streams from server
let stderr = session.accept_uni().await?; // Server -> Client unidirectional
let (recv, send) = session.accept_bi().await?; // Bidirectional

Server Side

// Session with full context and protocol access
fn handle_session(session: fastn_p2p::server::Session<Protocol>) {
    let ctx = session.context(); // fastn-context for debugging/cancellation
    let peer = session.peer();   // Client's public key
    
    // Main streams
    session.send  // Send to client (stdout)
    session.recv  // Receive from client (stdin)
    
    // Open additional streams back to client
    let stderr = session.open_uni().await?; // Server -> Client
    let (send, recv) = session.open_bi().await?; // Bidirectional
    
    // Convert to RPC pattern if needed
    let request = session.into_request(); // Full compatibility with existing handlers
}

Key Features

  • 🔄 Streaming Foundation: Complete client/server session types with TODO implementation stubs
  • 🔗 Context Integration: Sessions integrate with fastn-context for debugging and cancellation
  • 🔙 RPC Compatibility: Session::into_request() preserves existing Request-based patterns
  • 📡 Multi-stream Support: Unidirectional and bidirectional streams for stderr, additional channels
  • ✅ Zero Breaking Changes: All existing fastn_p2p::call() usage continues unchanged
  • 📦 Modular API: Clean client:: and server:: module exports

🏗️ Implementation Status

  • Type Definitions: Complete API surface with proper error types
  • fastn-context Integration: Sessions have context methods and dependency
  • Build Success: All code compiles without errors (TODO stubs in place)
  • 🔧 Next: Fill in TODO implementations with actual iroh streaming

📊 Changes

  • 7 files changed: +178 insertions, -54 deletions
  • New: client::connect(), client::Session, server::Session
  • Enhanced: Modular client/server modules, fastn-context dependency
  • Preserved: All existing Request/Response API and call() function

This provides the complete API foundation for streaming P2P sessions needed for remote shell functionality, with clear TODO markers ready for actual iroh implementation.

🤖 Generated with Claude Code

…d fastn-context integration

Add streaming API for P2P sessions alongside existing request/response pattern.

Changes:
- Add fastn-context dependency for session context management
- Implement client::connect() for streaming sessions
- Add client::Session with stdin/stdout streams and uni/bi stream support
- Implement server::Session with protocol, context, and stream management
- Add Session::into_request() for RPC compatibility
- Export new modular client and server modules
- Maintain backward compatibility with existing call() API

This enables remote shell and streaming use cases while preserving
existing fastn-p2p functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@amitu amitu changed the base branch from main to feature/fastn-p2p-streaming September 17, 2025 09:46
@amitu amitu merged commit 505f5ca into feature/fastn-p2p-streaming Sep 17, 2025
2 of 4 checks passed
@amitu amitu deleted the feat/fastn-p2p-streaming branch September 17, 2025 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants