Skip to content

Commit

Permalink
WIP: Add socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
harishmohanraj committed Nov 23, 2023
1 parent dde0206 commit 18ed6d1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ app chatApp {
("stripe", "11.15.0"),
("markdown-to-jsx", "7.3.2"),
],
webSocket: {
fn: import { webSocketFn } from "@server/webSocket.js"
},
}

/* 💽 Wasp defines DB entities via Prisma Database Models:
Expand Down
9 changes: 9 additions & 0 deletions src/client/components/ConversationWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useQuery } from "@wasp/queries";
import updateConversation from "@wasp/actions/updateConversation";
import getAgentResponse from "@wasp/actions/getAgentResponse";
import getConversations from "@wasp/queries/getConversations";
import { useSocket, useSocketListener } from "@wasp/webSocket";

import ConversationsList from "./ConversationList";
import Loader from "./Loader";
Expand Down Expand Up @@ -41,8 +42,16 @@ export default function ConversationWrapper() {
// @ts-ignore
const { id } = useParams();
const [isLoading, setIsLoading] = useState(false);
// const { socket, isConnected } = useSocket();
// const { refreshChatUI, setRefreshChatUI } = useState(false);
const chatContainerRef = useRef(null);

// useSocketListener("updateChatUI", updateChatUI);

// function updateChatUI() {
// setRefreshChatUI(true);
// }

const loginMsgQuery: any = getQueryParam("msg");
const formInputRef = useCallback(
(node: any) => {
Expand Down
70 changes: 70 additions & 0 deletions src/server/webSocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import HttpError from "@wasp/core/HttpError.js";

const ADS_SERVER_URL = process.env.ADS_SERVER_URL || "http://127.0.0.1:9000";

export const webSocketFn = (io, context) => {
io.on("connection", async (socket) => {
if (socket.data.user) {
const userEmail = socket.data.user.email;
console.log("========");
console.log("a user connected: ", userEmail);

// Check for updates every 5 seconds
const updateInterval = setInterval(async () => {
console.log("Check for inprogress tasks update");
const conversations = await context.entities.Conversation.findMany({
where: { userId: socket.data.user.id, status: "inprogress" },
});

conversations.length > 0 &&
conversations.forEach(async function (conversation) {
try {
const payload = {
conversation_id: conversation.id,
};
const response = await fetch(
`${ADS_SERVER_URL}/openai/get-team-status`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}
);

const json = await response.json();

// Todo: check how throwing the below error affects the user experience
if (!response.ok) {
const errorMsg =
json.detail ||
`HTTP error with status code ${response.status}`;
console.error("Server Error:", errorMsg);
throw new Error(errorMsg);
}

const conversation_status = json["status"];
if (conversation_status === "ready") {
const updated_conversation = conversation.conversation.concat([
{ role: "assistant", content: json["msg"] },
]);
await context.entities.Conversation.update({
where: {
// userId: socket.data.user.id,
id: conversation.id,
},
data: {
conversation: updated_conversation,
status: conversation_status,
},
});

// io.emit("updateChatUI");
}
} catch (error) {
throw new HttpError(500, error);
}
});
}, 5000);
}
});
};

0 comments on commit 18ed6d1

Please sign in to comment.