Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions frontend/app/conversation/[username].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MessageBubble } from "@/components/MessageBubble";
import { MessageComposer } from "@/components/MessageComposer";
import { useAppColors } from "@/hooks/useAppColors";
import { useAuth } from "@/contexts/AuthContext";
import { getDirectMessages, createDirectMessage } from "@/services/api";
import { API_BASE_URL, getDirectMessages, createDirectMessage } from "@/services/api";
import { ApiDirectMessage } from "@/constants/Types";

type MessageWithState = ApiDirectMessage & {
Expand All @@ -26,6 +26,19 @@ type MessageWithState = ApiDirectMessage & {
tempId?: string;
};

const getWebSocketBaseUrl = (baseUrl?: string) => {
if (!baseUrl) {
return "";
}
if (baseUrl.startsWith("https://")) {
return `wss://${baseUrl.slice("https://".length)}`;
}
if (baseUrl.startsWith("http://")) {
return `ws://${baseUrl.slice("http://".length)}`;
}
return baseUrl;
Comment on lines +33 to +39
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getWebSocketBaseUrl is now duplicated here and in frontend/app/terminal.tsx. To avoid drift (e.g., future protocol/path handling changes), consider extracting this helper into a shared module (e.g., @/services/api or a @/utils/websocket helper) and importing it in both places.

Suggested change
if (baseUrl.startsWith("https://")) {
return `wss://${baseUrl.slice("https://".length)}`;
}
if (baseUrl.startsWith("http://")) {
return `ws://${baseUrl.slice("http://".length)}`;
}
return baseUrl;
try {
const url = new URL(baseUrl);
if (url.protocol === "https:") {
url.protocol = "wss:";
} else if (url.protocol === "http:") {
url.protocol = "ws:";
}
return url.toString();
} catch {
// Fallback for non-standard or relative URLs: map http/https prefixes to ws/wss.
if (baseUrl.startsWith("https://")) {
return `wss://${baseUrl.slice("https://".length)}`;
}
if (baseUrl.startsWith("http://")) {
return `ws://${baseUrl.slice("http://".length)}`;
}
return baseUrl;
}

Copilot uses AI. Check for mistakes.
};

export default function ConversationScreen() {
const colors = useAppColors();
const insets = useSafeAreaInsets();
Expand Down Expand Up @@ -74,14 +87,11 @@ export default function ConversationScreen() {
let reconnectAttempts = 0;
let reconnectTimeoutId: ReturnType<typeof setTimeout> | null = null;
let isActive = true;

// Connect to WebSocket (reuse existing connection pattern from terminal)
const protocol = __DEV__ ? "ws" : "wss";
const host = __DEV__ ? "10.0.2.2:8080" : "devbits.ddns.net";
const wsUrl = `${protocol}://${host}/messages/${encodeURIComponent(
// Connect to WebSocket using API_BASE_URL
const wsBase = getWebSocketBaseUrl(API_BASE_URL);
const wsUrl = `${wsBase}/messages/${encodeURIComponent(
user.username
)}/stream?token=${encodeURIComponent(token)}`;

const connect = () => {
if (!isActive) {
return;
Expand Down