Skip to content

Commit

Permalink
feat: use websocket
Browse files Browse the repository at this point in the history
related #61
  • Loading branch information
pociej committed May 10, 2024
1 parent a2dcd22 commit 7097c08
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 28 deletions.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@fastify/cors": "^9.0.1",
"@fastify/multipart": "^8.1.0",
"@fastify/sensible": "^5.5.0",
"@fastify/websocket": "^10.0.1",
"@golem-sdk/golem-js": "workspace:*",
"@golem-sdk/task-executor": "workspace:*",
"@types/jsonwebtoken": "^9.0.6",
Expand All @@ -41,6 +42,7 @@
},
"devDependencies": {
"@types/node": "^20.11.20",
"@types/ws": "^8.5.10",
"concurrently": "^8.2.2",
"nodemon": "^3.1.0",
"prisma": "^5.10.2",
Expand Down
3 changes: 2 additions & 1 deletion backend/src/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { userService } from "./services/user/routes.js";
import { FastifySSEPlugin } from "fastify-sse-v2";
import fastifyMultipart from "@fastify/multipart";
import { Yagna } from "./services/yagna/routes.js";
import websocket from '@fastify/websocket'

export const startupFastifyServer = async (): Promise<FastifyInstance> => {
const fastify = Fastify({
Expand All @@ -22,7 +23,7 @@ export const startupFastifyServer = async (): Promise<FastifyInstance> => {
fastify.register(fastifySensible);
fastify.register(fastifyMultipart);
fastify.register(FastifySSEPlugin);

fastify.register(websocket);
fastify.register(cors, {
origin: "*",
});
Expand Down
15 changes: 11 additions & 4 deletions backend/src/services/file/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ export const fileService = fastifyPlugin(
reply.send({ message: "File uploaded" });
},
});
fastify.get("/scan-result", {
handler: (request, reply) => {
fastify.get("/ws", {websocket : true}, (socket) => {
setInterval( () => {
console.log("sending message")
socket.send("lalala");
},2000)
}
),
fastify.get("/scan-result", {websocket : true},
(socket) => {
container.cradle.fileService.resultStream.subscribe((result) => {
debugLog("publishing result on sse channel",result);
reply.sse({ data: JSON.stringify(result), id: uuidv4() });
socket.send(JSON.stringify(result));
});
},
});
);

done();
}
Expand Down
3 changes: 3 additions & 0 deletions backend/src/services/payment/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const paymentService = fastifyPlugin(
//@ts-ignore TODO: add declaration in auth module so ts-ignore is not needed
onRequest: [fastify.authenticate],
handler: async (request, reply) => {
console.log("wtf creating deposit");
const paymentService = container.cradle.paymentService;
// @ts-ignore
// TODO: make sure request.body is the right type
Expand All @@ -26,6 +27,8 @@ export const paymentService = fastifyPlugin(
// @ts-ignore
request.body.nonce
);

console.log("res",res);
return res;
},
});
Expand Down
7 changes: 5 additions & 2 deletions backend/src/services/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export const paymentService = (
saveDeposit: async (userId: string, nonce: string) => {
const userService = container.cradle.userService;
const user = await userService.findById(userId);
console.log("saving deposit");
if (!user) {
throw new Error(`User not found with id ${userId}`);
}

console.log('has user')
const data = await publicClient.readContract({
address: contractAddress,
abi: abi,
Expand All @@ -45,8 +46,8 @@ export const paymentService = (
`Deposit not found with nonce ${nonce} and funder ${user.walletAddress}`
);
}
console.log("has data",data);
// @ts-ignore

const { amount, feeAmount } = data;

const feeRatio = Number(BigDecimal.default.divide(feeAmount, amount));
Expand All @@ -59,6 +60,8 @@ export const paymentService = (
if (feeRatio < Number(serviceFee)) {
documentDeposit.isValid = false;
}

console.log("eeeee");
userService.addDeposit(userId, documentDeposit);
},
};
Expand Down
1 change: 1 addition & 0 deletions backend/src/services/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const userService: IUserService = {
},

addDeposit: async (userId: string, deposit: Deposit) => {
console.log("adding deposit");
await userModel.updateOne(
{ _id: userId },
{
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/hooks/useScanResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export const useScanResults = () => {
const scanResults = useRef<object[]>([]);
const { removeFile } = useFileUploader();
const { data, error } = useSWRSubscription("scanResult", (key, { next }) => {
const eventSource = new EventSource(
`${import.meta.env.VITE_BACKEND_URL}/scan-result`
const eventSource = new WebSocket(
`${import.meta.env.VITE_BACKEND_URL.replace('http','ws')}/scan-result`
);
eventSource.onmessage = (event) => {

eventSource.addEventListener('message',(event) => {
console.log("event",event);
const file = JSON.parse(event.data);
const newResults = R.uniqWith(
// @ts-ignore
Expand All @@ -19,7 +21,7 @@ export const useScanResults = () => {
scanResults.current = newResults;
removeFile(file.id);
next(null, newResults);
};
});

return () => eventSource.close();
});
Expand Down
39 changes: 22 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7097c08

Please sign in to comment.