I have a realtime app where users are clicking on a button at the exact same time. It is a rideshare app where a ride will show up on all the users's screens, and then 2 users will basically simultaneously push the "accept ride" button. This creates problems because it saves the first user onto the ride via a database save call, but then the second user oversaves the first user. Once a user accepts the ride, another user should not be able to accept the ride. It creates major problems because the first user should just "accept the ride" while, with the second driver, it should just tell them "another driver is accepting the ride". The problem is that I can not even run a query to check if the ride already has a driver because this is all happening so quickly. The first user will hit "accept ride", then it will save them to the ride. The second user will hit "accept ride", and it will check if the ride already has a driver. It doesn't yet because the first save is still finishing. Then the second user is oversaved on the ride. It just is happening way to simultaneously where queries don't really solve the problem.
Sorry if this is a confusing explanation. I have never had to deal with this realtime of a problem, so I am not sure where to start. I think I need to build some queue or something that only lets this happen once at a time. Any direction of what to even google would be helpful. Thank you! My backend is written in node.js and I use MongoDB on Heroku.
You need an atomic check and set operation in your database so in one atomic database operation, you can verify that it's not already accepted and, if not, accept it. That will only allow one person to accept it, any others will fail because it's already accepted and the API can feed that back to the user interface. The key word here is "atomic" and how you achieve it depends upon the specific database. For MongoDB, see Mongo any way to do atomic check and set.
Here's another reference: Help writing an atomic update in mongodb.
These solutions use mongodb's findandmodify so presumably, you would attempt to find a document with this id and that is also not accepted and if found, you would modify it to be accepted. Then, since the findandmodify is atomic, nobody else can get in between your find and your modify so when their findandmodify gets a turn, they won't find a document that is both the right id and is not accepted because someone else accepted it before they got in.
Related
I have a route to accept request from user and return data to the user. There are the step
User send request to my app
Read data from database
Update my database
Return data to user
I want step 3 and step 4 above happen at the same time so the user will not be waiting for the update operation. How can I make this?
The thing you might be looking is called background task and luckily, FastAPI has an awesome documentation about how to implement that. Here you have!
But be careful, your title may be implying something different from your real question. The async/await will keep you away from blocking your server, but with background tasks you can make your user wait less time for their response. Saying that, if you're going to send a response without knowing anything else, remember to send a 202 status code (Accepted).
I want to make an app where people can text (sms) their name to sign a petition and then their name would be added to a list of signees on a website for the petition.
The sms messages would be handled by Twilio and then be processed by a Node.js app. I would like to use Angular on the front end.
Question: What would be the best way to store the names? Do I need something like MongoDB or would a database be overkill? I'd also like to verify that only one name per phone number is entered.
If you don't use a database you'll need to store it in memory. This will mean when you restart the server or it reboots you will lose your names.
So the short answer is yes you will need a database.
As for checking if there are duplicates there are many method with mongodb.
You can use a find query first to check that it does not exist already.
You could use upsert which will replace a previous one matching one.
However you can use any type of database that you wish.
I'd like to vote in favor of a relational database, probably it's a good fit for your kind of project.
Take in account that you can also store some interesting fields that comes with the Twilio request like country and state to list some.
You can take a look at this tutorial where not only you can get the SMS but reply to the user to have a smooth user experience in the process: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-node-js
I’m building an agent on API.ai where I ask a user a question. I’m not expecting them to answer the question back to my agent. However they may wish to follow up this question later on by asking for some more information. If I ‘end the conversation’ in my intent they can’t then do something such as say ‘tell me more’ without invoking my action again from scratch (in which case all context is lost), but similarly if they don’t say anything, then (on google home at least) the question gets repeated as it's expecting a response.
Is there anyway I could do this?
Actions are conversational experiences. Typically your app would ask a question and the user would provide a response. Once the user exits your app, the conversational context goes back to the assistant.
If you want to provide a quick way to let the user engage with your app again, then consider implementing support for deep links: https://developers.google.com/actions/apiai/define-actions#define_additional_actions
In addition to what Leon has said, you could also manage the context of the user yourself (instead of relying on API.AI's Contexts) and key off the anonymous userid that you get with each request.
This way they can deep-link back to ask you a followup question, and you know "who" is returning and where the conversation last stood when you gave a reply.
I understand that what you basically want is to create an intent in which what the user will say is not predictable (they may not say anything at all).
In that case , you can simply end the response with a prompt to specify it. "...Do you want to ask something more ". If user says "no" , end the conversation in a different intent. Otherwise carry on with the flow.
I am developing a Rest API using node js, mongo and express as technologies. My models include users, venues, etc. In addition each user has states. Examples of states could be when a user signup the first state is 'new_user', after one week the state must be 'first_week_user' and so on.
The purpose of these states is to notify the user according to his or her state. For example if a user like a picture and the user is in the first week (he has the 'first_week' state) so an email must be sent to him. I am in the design stage right now, so I want to know if somebody had to face the same issue before.
The design that I have in mind is to put a notification_profile inside the user object and using a cron job to check the state and the actions of the day and according to that send the emails/push notifications.
What do you think? Are there a better option? e.g. I can have an email API and queue the emails hitting this API. Do you know where I can find information about design patterns facing this problem?
Thanks a lot for your help.
Without more detail, this sounds like you need the Observer pattern.
Essentially, your Email component would subscribe to each Person object's like(photo photo) event, and either execute an email-send job immediately, or schedule the job to run later, as part of a batch.
One way to specify the state transitions would be as a hierarchical state machine. See http://www.eventhelix.com/realtimemantra/hierarchicalstatemachine.htm#.VNJIflXF--o and http://en.wikipedia.org/wiki/UML_state_machine
I don't have a good node.js example but here's a C# implementation that also includes the concept of timed events. Essentially the state machine keeps track of a NextTimedEventAt so you can efficiently pull it back out of a database at the right time to fire a time-based event.
Actions happen on state transitions: as you enter a state or leave a state.
Just wondering about the best way to subscribe to my CouchDB data store, so that if a document in couch is updated, the KO view will also update (automagically). Is this something that's even possible?
Below is what I have so far, which simply get the user name from the user_info document.
$.getJSON('http://localhost/couchdb/user_info', function(data) {
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
Any help would be greatly appreciated!
CouchDB supports notifications when documents change: the changes feed.
You can poll the changes feed, with a ?since=X parameter to receive only updates since X.
You can also "long poll" the feed by adding &feed=longpoll. If there are no changes yet, CouchDB will receive your query but not answer until finally a change comes on.
Or, you can have a full COMET-style feed by instead adding &feed=continuous. That is similar to longpoll, however CouchDB will never close the connection. Every time a change happens, it will send you the JSON and then continue waiting.
Finally, you can be notified when anything changes in the database, or you can specify a Javascript filter to run on the server (&filter=designdoc/filtername). You will only receive notifications if the filter approves.
Have you looked at http://hood.ie/ it woks well. I'm also running hoodie as an os_daemons service from within my couchdb.
It's nice.