import styles from '../styles/Chat.module.css' export default function Info() { return (

📚 How api-ape Works

{/* Top Left: Key Concepts */}

💡 Key Concepts

              {`• Proxy Pattern: api.message() → api/message.js
• Auto-wiring: Drop files in api/ folder, they become endpoints
• Promises: All calls return Promises automatically
• Pub/Sub: Use ape.publish.channel(data) for channel subscribers
• Context: this.clients, this.clientId, this.req available in controllers
• Auto-reconnect: Client reconnects automatically on disconnect`}
            
{/* Top Right: Data Flow */}

🔄 Data Flow

{/* Column Headers */}
Client
Server
{/* Step 1: Client sends */}
api.message(data)
Send
{/* Step 2: Server receives */}
Return
api/message.js
{/* Step 3: Server sends to others */}
Send
Send to others
{/* Step 4: Other clients receive */}
Other clients
Receive
{/* Bottom Left: Client-Side */}

🔵 Client-Side (Browser)

              {`// 1. Initialize api-ape client
const client = await getApeClient()
const api = client.sender  // Proxy object

// 2. Call server function - property name = file path
//    api.message() → calls api/message.js
api.message({ user: 'Alice', text: 'Hello!' })
  .then(response => {
    // Server returned: { ok: true, message: {...} }
    console.log('Response:', response)
  })
  .catch(err => {
    // Server threw an error
    console.error('Error:', err)
  })

// 3. Listen for server messages
client.setOnReceiver('message', ({ data }) => {
  // Server sent to this client
  // This fires for clients the server sends to
  console.log('Message received:', data.message)
})`}
            
{/* Bottom Right: Server-Side */}

🟢 Server-Side (api/message.js)

              {`// File: api/message.js
// This function is called when client does: api.message(data)

module.exports = function message(data) {
  const { user, text } = data

  // Validate input
  if (!user || !text) {
    throw new Error('Missing user or text')
  }

  const msg = {
    user,
    text,
    time: new Date().toISOString()
  }

  // Send to ALL OTHER clients (not the sender)
  this.clients.forEach((client) => {
    if (client.clientId !== this.clientId) {
      client.send('message', { message: msg })
    }
  })

  // Return response to sender (fulfills Promise)
  return { ok: true, message: msg }
}`}
            
) }