Skip to main content

Server Adapters

Server adapters provide the interface between WSX and different web frameworks. They handle WebSocket connections, message routing, and framework-specific integration.

Built-in Adapters

Express Adapter

Hono Adapter

Adapter Interface

WSXServerAdapter Interface

WSXConnection Interface

Creating Custom Adapters

Basic Adapter Structure

Fastify Adapter Example

Next.js Adapter Example

Adapter Features

Authentication Integration

Rate Limiting Integration

Compression Support

Testing Adapters

Adapter Testing Framework

Best Practices

  1. Error Handling: Implement robust error handling for connection issues
  2. Security: Validate and authenticate connections appropriately
  3. Performance: Optimize for your framework’s characteristics
  4. Compatibility: Ensure compatibility with framework middleware
  5. Testing: Write comprehensive tests for adapter functionality
  6. Documentation: Document adapter-specific configuration options
  7. Monitoring: Add logging and metrics for adapter performance

Framework-Specific Considerations

Express

  • Use express-ws or similar WebSocket middleware
  • Integrate with Express middleware stack
  • Handle HTTP upgrade requests properly

Hono

  • Use Hono’s WebSocket support
  • Consider edge runtime compatibility
  • Optimize for serverless deployment

Fastify

  • Use @fastify/websocket plugin
  • Leverage Fastify’s plugin system
  • Maintain Fastify’s performance characteristics

Next.js

  • Use Socket.IO for broader compatibility
  • Handle API routes integration
  • Consider serverless limitations

Next Steps