Ian
Ian
TTCTheo's Typesafe Cult
Created by Remiwi on 6/30/2024 in #questions
How to keep a local DB in sync with a DB I don't own?
This is a very hard problem to solve ^^ First of all, what do you want to happen when there's a conflict between local & server? (Say for example, two people edit the same thing)
5 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
i jsut using nextjs fro both frontend and backend
In that case, I'm assuming all your requests are serverless, meaning you have no state on the server side (because every request lives in its own serverless function) To achieve long polling you need state, because the endpoint that receives the paypal event somehow needs to communicatie with the long polling request. Here are some options: Redis Using redis pub/sub, the endpoint that receives the paypal request can publish an event to redis, and the long polling endpoint can subscribe to events in redis. Mongo change streams I was looking around, and it seems that mongo supports "change streams" https://www.mongodb.com/docs/manual/changeStreams/ Never tried them, but perhaps they can be used by the polling endpoint to listen to events in mongo There are many other ways to achieve the same result, but realise that normal polling will still be the simplest solution. Make sure you ask yourself the question what the added benefit is of users being redirected away from the checkout page instantly, instead of having to wait 3 seconds
54 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
how would long polling work in this scenario?
Happy to help a bit here, but what does your stack look like? ^^ If you want to do long-polling you'll need state. Super simple example of long polling to explain how it works (you can definitely use a library if you want): Server:
const clients = {};

app.get("/new-events", (req, res) => {
const sendEvent = (data) => {
delete clients[req.body.id];
res.json(data);
}
clients[req.body.id] = sendEvent;
});

app.post("/send-event", (req, res) => {
const client = [req.body.id];
if (!client) {
return res.send("That user is not active");
}
client.sendEvent(req.body.data);
return res.send("Sent!")
});
const clients = {};

app.get("/new-events", (req, res) => {
const sendEvent = (data) => {
delete clients[req.body.id];
res.json(data);
}
clients[req.body.id] = sendEvent;
});

app.post("/send-event", (req, res) => {
const client = [req.body.id];
if (!client) {
return res.send("That user is not active");
}
client.sendEvent(req.body.data);
return res.send("Sent!")
});
Note: this is a simplified example and needs more work to be used in production.
54 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
Sidenote: this would most easily be handled by a normal polling imo. Waiting 3 seconds here after payment is perfectly ok 🙃 Alternatively you could also improve simple polling by holding the request a bit on the server, and checking multiple times like so:
app.get('/check-payment-status', (req, res) => {
for (let i = 0; i < 10; i++) {
if (await isPaymentComplete()) {
return res.send("Payment complete!")
}
await new Promise(r => setTimeout(r, 500))
}
res.send('Payment not yet complete');
});
app.get('/check-payment-status', (req, res) => {
for (let i = 0; i < 10; i++) {
if (await isPaymentComplete()) {
return res.send("Payment complete!")
}
await new Promise(r => setTimeout(r, 500))
}
res.send('Payment not yet complete');
});
That way you skip the overhead of going to the server and back (which can often be an issue in serverless setups)
54 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
Because theyre just http requests
54 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
If your app, in general, needs a lot of interactivity, then web sockets are great. Otherwise long polling is imo the easiest to set up.
54 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
Long polling doesn't really have a long delay as long as there aren't many messages in rapid succession. The way long polling works is that the client sends a request to the server over http. Then the server holds the request without responding until it needs to send something. Every so often this is redone to not reach any time-out limits, but it's quite instant for all intents and purposes
54 replies
TTCTheo's Typesafe Cult
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 6/20/2024 in #questions
Post Quantum Encryption
Sadly I don't know any mature node implementations, although I haven't looked much
9 replies
TTCTheo's Typesafe Cult
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 6/20/2024 in #questions
Post Quantum Encryption
Well node supports webassembly, but I've never tried it out. https://nodejs.org/en/learn/getting-started/nodejs-with-webassembly Otherwise it's always an option to run a separate micro service in another language just for this purpose. 🙂
9 replies
TTCTheo's Typesafe Cult
Created by Elite on 6/24/2024 in #questions
instant updates from route handler to frontend
Websockets, long polling, and server sent events would all solve this issue, and would feel nearly instant to the user. 🙂
54 replies
TTCTheo's Typesafe Cult
Created by FleetAdmiralJakob 🗕 🗗 🗙 on 6/20/2024 in #questions
Post Quantum Encryption
Are you looking for a web solution or an android/ios solution? My knowledge on the subject is quite limited, but have you tried taking an existing solution like liboqs and compiling it to webassembly?
9 replies
TTCTheo's Typesafe Cult
Created by Gnommon on 6/17/2024 in #questions
Refactoring for Dependency Injection (Circular Dependecies)
From experience, I've noticed that focusing on design principles often brings worse and less maintainable code. Instead, I would urge you to look for problems and difficulties that you're having, and how to fix those specifically. Look at your project, and which problems you want to solve, and only then, look for solutions. Not the other way around. PS: Do you need an interface for the UserRepository here?
5 replies
TTCTheo's Typesafe Cult
Created by Gnommon on 6/17/2024 in #questions
Refactoring for Dependency Injection (Circular Dependecies)
First, as a side-note, the easiest way to handle circular dependency is to not have any. This is of course not always possible, and I don't have enough detail, but I would ask myself if the other service you talk about really needs to handle auth at this layer, and if so, which part of auth specifically? That said, one way to handle circular dependencies in this context is by having lazy dependencies. In this case the dependency would only be created when it is used. This allows the both dependencies to depend on each-other in the same way as if you created a dependency per function, without having to create a class for every function (which is also a possible solution, but one you already thought of, so I'm providing an alternative).
5 replies