Streaming bug using sockets

Hi everyone,

Has anyone had any issue with responses in streaming? I find it quite regularly drops out words and spelling.

My developer has created the code to use Sockets and rooms and im wondering if there’s a better way to handle the streaming.

this is some of the code for reference

const ChatWindow = () => {
	const {
		selectedChat,
		setSelectedChat,
		createChat,
		question,
		sendChat,
		selectChat,
		isMessageLoading,
	} = useChat();
	const { prompts } = useAssistant();
	const { user } = useUser();
	const { theme } = useTheme();
	const [searchParams] = useSearchParams();
	const [response, setResponse] = useState<string>("");
	const [isUserScrolling, setIsUserScrolling] = useState<boolean>(false);
	const [showPrompts, setShowPrompts] = useState<boolean>(true);
	const hiddenTextAreaRef = useRef<HTMLTextAreaElement>(null);
	const socketRef = useRef<Socket | null>(null);
	const messagesEndRef = useRef<HTMLDivElement | null>(null);
	const chatContainerRef = useRef<HTMLDivElement | null>(null);

	// Handle socket connection and disconnection on chat change
	useEffect(() => {
		if (selectedChat) {
			// Disconnect any existing socket
			if (socketRef.current) {
				socketRef.current.disconnect();
				socketRef.current = null;
			}

			// Initialize new socket connection for the current chat
			socketRef.current = io(process.env.REACT_APP_API_BASE_URL ?? "");

			// Set up socket listeners for responses and stream end
			const handleOpenAiResponse = (fullResponse: string) => {
				const cleanedResponse = fullResponse
					.replace(/```markdown|```/g, "")
					.trim();
				console.log(`Received response for chat: ${selectedChat?._id}`);

				setSelectedChat((currentChat: any) => {
					if (!currentChat || currentChat._id !== selectedChat?._id)
						return currentChat; // Update only the current chat
					let newMessages = currentChat.messages.slice();
					newMessages.pop();
					const newMessage = {
						_id: Date.now().toString(),
						question: question,
						answer: cleanedResponse ? cleanedResponse : "",
					};
					return {
						...currentChat,
						messages: [...newMessages, newMessage],
					};
				});
			};

			const handleStreamEnd = (fullResponse: string) => {
				const cleanedResponse = fullResponse
					.replace(/```markdown|```/g, "")
					.trim();
				setResponse(cleanedResponse);
				console.log(`Stream ended for chat: ${selectedChat?._id}`);

				setSelectedChat((currentChat: any) => {
					if (!currentChat || currentChat._id !== selectedChat?._id)
						return currentChat; // Update only the current chat
					let newMessages = currentChat.messages.slice();
					newMessages.pop();
					const newMessage = {
						_id: Date.now().toString(),
						question: question,
						answer: cleanedResponse,
					};
					return {
						...currentChat,
						messages: [...newMessages, newMessage],
					};
				});
			};

			// Add socket event listeners
			socketRef.current.on("openai_response", handleOpenAiResponse);
			socketRef.current.on("stream_end", handleStreamEnd);

			// Join the selected chat room
			socketRef.current.emit("joinRoom", selectedChat._id);
			console.log(`Joined room: ${selectedChat._id}`);

			// Clean up listeners and disconnect socket when selectedChat changes or component unmounts
			return () => {
				if (socketRef.current) {
					socketRef.current.off("openai_response", handleOpenAiResponse);
					socketRef.current.off("stream_end", handleStreamEnd);
					socketRef.current.disconnect();
					socketRef.current = null;
				}
			};
		}
	}, [selectedChat, question, setSelectedChat]);

	// Monitor for changes in the URL query to select a chat
	useEffect(() => {
		const chatId = searchParams.get("chat");
		if (chatId) {
			selectChat(chatId);
		}
	}, [searchParams]);

can you confirm that you are receiving the complete response from openai in the backend prior to sending back to the frontend using socket?

1 Like

Hi there thanks for your reply @supershaneski

I havent been able to replicate the problem in development so far … But i have attached a screen shot of my terminal response.

The image looks like its multiple response but this is actually the one response when i asked it to create a single story.

So it makes me think that the socked or room is every now and then closing and opening very quickly which is what is causing a work or spelling mistake. Or the chunk text is getting chopped off because of the new room. Hopefully that makes sense … Im a bit out of my depth on this one