Reconnect to RealTime API

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();
    };
  }, []);```
1 Like

You’ll need to manually add the history to retain context. See: https://platform.openai.com/docs/guides/realtime/continuing-conversations

2 Likes

Is this link working? In my case it just landing me to Realtime docs page. There is no option for continuing conversation

1 Like