> ## Documentation Index
> Fetch the complete documentation index at: https://haiprotocol.com/llms.txt
> Use this file to discover all available pages before exploring further.

# HAIP 1 archive — Transport Mechanisms

> Historical HAIP 1 documentation. Unsupported and incompatible with HAIP 2.

<Warning>
  **Unsupported HAIP 1 archive.** This is historical chat and streaming documentation, not the HAIP 2 review protocol. Its APIs, package examples and security advice must not be used for a HAIP 2 deployment. Start with the [current HAIP 2 guides](/index).
</Warning>

## Overview

HAIP is transport-agnostic, supporting multiple communication protocols. Choose the transport that best fits your use case and infrastructure requirements.

## WebSocket (Recommended)

WebSockets provide bidirectional, real-time communication with low latency and full-duplex capabilities.

### Advantages

* **Bidirectional**: Both client and server can send messages at any time
* **Low Latency**: Minimal overhead for real-time applications
* **Full-Duplex**: Simultaneous sending and receiving
* **Wide Support**: Available in all modern browsers and platforms

See the [WebSocket Implementation](/archive/v1/protocol/essentials/websocket) guide for detailed implementation examples.

## Server-Sent Events (SSE)

SSE provides one-way streaming from server to client, ideal for scenarios where the client primarily receives data.

### Advantages

* **Simple**: Easy to implement and debug
* **Automatic Reconnection**: Built-in reconnection handling
* **HTTP Compatible**: Works through proxies and firewalls
* **Event-Driven**: Native event handling

See the [Server-Sent Events](/archive/v1/protocol/essentials/sse) guide for detailed implementation examples.

## HTTP Streaming

HTTP streaming provides a simple way to stream data over standard HTTP connections.

### Advantages

* **Universal**: Works with any HTTP client
* **Proxy Friendly**: Passes through corporate firewalls
* **Simple**: No special client libraries required
* **Compatible**: Works with existing HTTP infrastructure

See the [HTTP Streaming](/archive/v1/protocol/essentials/http-streaming) guide for detailed implementation examples.

## Message Bus Integration

HAIP can be integrated with message buses like Redis, RabbitMQ, or Apache Kafka.

### Redis Pub/Sub Example

```typescript theme={null}
import Redis from 'ioredis';

class HAIPRedisClient {
  private redis: Redis;
  private sessionId: string;
  private seqCounter = 1;

  constructor() {
    this.redis = new Redis();
    this.sessionId = this.generateUUID();
  }

  async connect() {
    // Subscribe to HAIP events
    await this.redis.subscribe('haip:events');

    this.redis.on('message', (channel, message) => {
      const haipMessage = JSON.parse(message);
      this.handleMessage(haipMessage);
    });

    // Send handshake
    await this.sendHandshake();
  }

  private async sendHandshake() {
    const handshake = {
      id: this.generateUUID(),
      session: this.sessionId,
      seq: "1",
      ts: Date.now().toString(),
      channel: "SYSTEM",
      type: "HAI",
      payload: {
        haip_version: "1.1.2",
        accept_major: [1],
        accept_events: ["HAI", "MESSAGE_START", "MESSAGE_PART", "MESSAGE_END"]
      }
    };

    await this.redis.publish('haip:handshake', JSON.stringify(handshake));
  }

  async sendMessage(text: string) {
    const message = {
      id: this.generateUUID(),
      session: this.sessionId,
      seq: this.seqCounter++.toString(),
      ts: Date.now().toString(),
      channel: "USER",
      type: "MESSAGE_START",
      payload: {
        message_id: this.generateUUID(),
        author: "USER",
        text: text
      }
    };

    await this.redis.publish('haip:message', JSON.stringify(message));
  }
}
```

## Transport Selection Guide

<CardGroup cols={2}>
  <Card title="WebSocket" icon="plug">
    **Best for**: Real-time chat, gaming, collaborative applications **Pros**:
    Bidirectional, low latency, full-duplex **Cons**: More complex, requires
    WebSocket support
  </Card>

  {" "}

  <Card title="Server-Sent Events" icon="radio">
    **Best for**: Notifications, live updates, one-way streaming **Pros**: Simple,
    automatic reconnection, HTTP compatible **Cons**: One-way only, limited
    browser support
  </Card>

  {" "}

  <Card title="HTTP Streaming" icon="arrow-right">
    **Best for**: Simple integrations, proxy environments **Pros**: Universal,
    proxy friendly, simple **Cons**: Higher latency, no binary support
  </Card>

  <Card title="Message Bus" icon="network-wired">
    **Best for**: Microservices, distributed systems **Pros**: Scalable,
    decoupled, reliable **Cons**: Complex setup, additional infrastructure
  </Card>
</CardGroup>

## Connection Management

### Reconnection Strategies

```typescript theme={null}
class HAIPConnectionManager {
  private reconnectAttempts = 0;
  private maxReconnectAttempts = 5;
  private baseDelay = 1000;

  async reconnect() {
    if (this.reconnectAttempts >= this.maxReconnectAttempts) {
      throw new Error("Max reconnection attempts exceeded");
    }

    const delay = this.baseDelay * Math.pow(2, this.reconnectAttempts);
    this.reconnectAttempts++;

    await new Promise((resolve) => setTimeout(resolve, delay));

    try {
      await this.connect();
      this.reconnectAttempts = 0; // Reset on successful connection
    } catch (error) {
      console.error("Reconnection failed:", error);
      await this.reconnect();
    }
  }
}
```

### Health Monitoring

```typescript theme={null}
class HAIPHealthMonitor {
  private pingInterval: NodeJS.Timeout | null = null;
  private lastPong: number = 0;

  startPing() {
    this.pingInterval = setInterval(() => {
      this.sendPing();
    }, 30000); // Ping every 30 seconds
  }

  private sendPing() {
    const ping = {
      id: this.generateUUID(),
      session: this.sessionId,
      seq: this.seqCounter++.toString(),
      ts: Date.now().toString(),
      channel: "SYSTEM",
      type: "PING",
      payload: {
        nonce: this.generateUUID()
      }
    };

    this.ws.send(JSON.stringify(ping));
  }

  handlePong() {
    this.lastPong = Date.now();
  }

  isHealthy(): boolean {
    return Date.now() - this.lastPong < 90000; // 90 seconds
  }
}
```
