Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup websockets communication between web and workers #171

Merged
merged 2 commits into from
Sep 16, 2024

Conversation

andresgutgon
Copy link
Contributor

@andresgutgon andresgutgon commented Sep 13, 2024

What?

We want to keep the client informed when evaluations are running. How many logs are created, and how many logs fail
This PR set up a new Express server that set up a Socket.IO websocket server with 2 endpoints /websocket and worker-websocket. The one for the web has secure cookies token auth and the one for workers share a secret token between Sockets server and workers.

I think this setup makes sense

Screen.Recording.2024-09-16.at.13.56.54.mov

TODO

  • Setup Sockets server
  • Setup setting token and refresh token cookies in web when user login with magic link.
  • Setup Sockets client in worker so it can emit events to the websockets server
  • Setup DOMAIN_NAME env variable.
  • Setup <WebsocketsProvider /> in React client so it can start listening to events
  • Make Websocket connection work WEB
  • Make Websocket connection work WORKERS
  • Run evaluation and see if it triggers a websocket to the frontend
  • Build UI to show evaluation progress.
  • WHY first time a evaluation is run total is 0? REVIEW this
  • Setup refresh websocket token server action
  • Review ENV variables to avoid breaking deploy with this PR
  • Implement logout lol we didn't have it
  • Delete Websocket cookies when logout

Next PR

  • Review all necessary env variables
  • Think about ws or wss (Secure). Maybe is not necessary because is behind a proxy cc @geclos
  • @socket.io/redis-adapter for pub/sub with Redis. Maybe? I don't think we need now if it works without it cc @geclos

@andresgutgon andresgutgon added the 🚧 wip Work in progress label Sep 13, 2024
@@ -45,6 +45,18 @@ export const runEvaluationJob = async (job: Job<RunEvaluationJobData>) => {
}

await progressTracker.incrementErrors()
} finally {
await progressTracker.decrementTotal()
Copy link
Contributor Author

@andresgutgon andresgutgon Sep 14, 2024

Choose a reason for hiding this comment

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

decrementTotal was only in the catch but I think is something we want to do always no?
Also I think we could make decrementTotal private and run it inside increamentErrors and the other method for success

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm no we only wanna do it when there's an error, if we succeed we don't wanna decrement the total

@@ -27,10 +27,12 @@
"@sentry/nextjs": "^8",
"@t3-oss/env-nextjs": "^0.10.1",
"ai": "^3.2.42",
"socket.io-react-hook": "^2.4.5",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not very complicated to integrate but this package is small and nice

@andresgutgon andresgutgon force-pushed the feature/spike-websockets branch 4 times, most recently from caced6f to dbaacf8 Compare September 15, 2024 11:54
@@ -42,6 +43,8 @@
"nprogress": "^0.2.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"socket.io-client": "^4.7.5",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🔥 Included in the hook

token,
cookiesOptions: {
secure: isProd,
domain: isProd ? `.${env.APP_DOMAIN}` : 'localhost',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CORS for all the subdomains of the app domain. This way we can share cookies between web client and websockets server as auth method

@andresgutgon andresgutgon force-pushed the feature/spike-websockets branch 2 times, most recently from 61e7179 to 6921e29 Compare September 16, 2024 10:55
@andresgutgon andresgutgon removed the 🚧 wip Work in progress label Sep 16, 2024
@andresgutgon andresgutgon force-pushed the feature/spike-websockets branch 3 times, most recently from 7d7f57c to 2e282eb Compare September 16, 2024 12:13
We want to keep the client informed when evaluations are running. How
many logs are created and how many logs fail.
This PR setup a new Express server that setup a Socket.IO websocket
server with 2 endpoints /websocket and worker-websocket. The one for web
has secure cookies token auth and the one for workers share a secret
token between Sockets server and workers.

I think this setup makes sense
@andresgutgon andresgutgon force-pushed the feature/spike-websockets branch from 2e282eb to 6ece440 Compare September 16, 2024 12:21
@@ -23,5 +24,6 @@ export default createEnv({
GATEWAY_HOSTNAME: process.env.GATEWAY_HOSTNAME,
GATEWAY_PORT: process.env.GATEWAY_PORT,
GATEWAY_SSL: process.env.GATEWAY_SSL,
WEBSOCKETS_SERVER: process.env.WEBSOCKETS_SERVER,
Copy link
Collaborator

Choose a reason for hiding this comment

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

no hay default aqui

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's on packages/env/src/index.ts

@andresgutgon andresgutgon force-pushed the feature/spike-websockets branch from a42ff43 to 9c43369 Compare September 16, 2024 13:09
@andresgutgon andresgutgon merged commit 1295d48 into main Sep 16, 2024
3 checks passed
@andresgutgon andresgutgon deleted the feature/spike-websockets branch September 16, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants