import styles from '../styles/Chat.module.css' export default function Info() { return (
{`• 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`}
{`// 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)
})`}
{`// 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 }
}`}