Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { useState } from 'react'
3import { useChat, webSocket } from '@tanstack/ai-react'
4
5export const Route = createFileRoute('/websocket-chat')({
6 component: WebSocketChatPage,
7})
8
9// A full-duplex WebSocket connection. `/api/chat-ws` (see
10// `src/lib/websocket-chat-plugin.ts`) records every chunk to a durability
11// log and serves `?offset=` resumes over the same socket protocol, so a
12// dropped connection reconnects and resumes automatically — nothing on the
13// client opts in beyond using `useChat` with `webSocket()`.
14const connection = webSocket('/api/chat-ws')
15
16function WebSocketChatPage() {
17 const { messages, sendMessage, isLoading, connectionStatus } = useChat({
18 connection,
19 })
20 const [input, setInput] = useState('Write a short haiku about WebSockets.')
21
22 const handleSubmit = (e: React.FormEvent) => {
23 e.preventDefault()
24 const text = input.trim()
25 if (!text || isLoading) return
26 setInput('')
27 void sendMessage(text)
28 }
29
30 return (
31 <div style={page}>
32 <h1>WebSocket chat</h1>
33 <p style={{ color: '#555' }}>
34 This chat talks to the model over a single full-duplex WebSocket instead
35 of HTTP/SSE. The server durably logs each chunk, so a dropped connection
36 reconnects with <code>?offset=</code> and resumes the run rather than
37 restarting the model — all through <code>useChat</code> with the{' '}
38 <code>webSocket()</code> connection adapter.
39 </p>
40
41 <div style={{ margin: '12px 0', color: '#888', fontSize: 13 }}>
42 connection: <code>{connectionStatus}</code>
43 </div>
44
45 <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
46 {messages.map((message) => (
47 <div
48 key={message.id}
49 style={message.role === 'user' ? userBubble : assistantBubble}
50 >
51 <div style={roleLabel}>{message.role}</div>
52 {message.parts.map((part, index) =>
53 part.type === 'text' && part.content ? (
54 <p
55 key={`${message.id}-${index}`}
56 style={{ margin: 0, whiteSpace: 'pre-wrap' }}
57 >
58 {part.content}
59 </p>
60 ) : null,
61 )}
62 </div>
63 ))}
64 </div>
65
66 <form
67 onSubmit={handleSubmit}
68 style={{ marginTop: 16, display: 'flex', gap: 8 }}
69 >
70 <input
71 value={input}
72 onChange={(e) => setInput(e.target.value)}
73 placeholder="Ask something…"
74 style={{ flex: 1, padding: 8 }}
75 />
76 <button type="submit" disabled={isLoading}>
77 {isLoading ? 'Streaming…' : 'Send'}
78 </button>
79 </form>
80 </div>
81 )
82}
83
84const page: React.CSSProperties = {
85 maxWidth: 720,
86 margin: '0 auto',
87 padding: 24,
88 fontFamily: 'system-ui, sans-serif',
89}
90
91const bubble: React.CSSProperties = {
92 borderRadius: 8,
93 padding: '10px 14px',
94 maxWidth: '85%',
95}
96
97const userBubble: React.CSSProperties = {
98 ...bubble,
99 alignSelf: 'flex-end',
100 background: '#eef2ff',
101}
102
103const assistantBubble: React.CSSProperties = {
104 ...bubble,
105 alignSelf: 'flex-start',
106 background: '#f6f6f6',
107}
108
109const roleLabel: React.CSSProperties = {
110 fontSize: 11,
111 textTransform: 'uppercase',
112 letterSpacing: '0.05em',
113 color: '#999',
114 marginBottom: 4,
115}