Here we connecting to Real-Time API. After it’s connected we start talking and getting response at some point it gets disconnected due to some reason. We need to reconnect. I couldn’t any way to reconnect to the session we were talking, to keep the context. It’s connecting again but to a new session, now we have to talk from start or create the context again.
useEffect(() => {
const wsUrl = 'wss://OPENAI_WS_URL.app';
ws.current = new WebSocket(wsUrl, null, {
headers: {
Authorization: `Bearer ${OPENAI_API_KEY}`,
'OpenAI-Beta': 'realtime=v1',
},
});
ws.current.onopen = () => {
setConnection('connected');
initConversation();
console.log('Connected to OpenAI Realtime API');
};
ws.current.onmessage = message => handleWebSocketMessage(message);
ws.current.onerror = error => console.error('WebSocket Error:', error);
ws.current.onclose = event => {
setConnection('disconnected');
};
return () => {
ws.current?.close();
};
}, []);```