I have a specific requirment for a web application which is build with Nodejs/MongoDb/Express/Angular. Application itself is a application for mentors and has a kind of schema like this:
var userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
required: true
},
username: {
type: String,
unique: true,
required: true
},
....
status: {type: String, enum: ["online", "offline"]},
schedule: [{
day: String,
StartTime: Date,
EndTime: Date
}]
...
})
Every user has its own schedule, for example working Every: Monday ,Wednesday ,Thursday From: 04:00 PM To: 07:00 PM.
Now, how can I change the status from Offline to Online as they submited their schedule. I mean from the example above, to change Every: Monday ,Wednesday ,Thursday at the specific time the user status on db from "offline" to "online" ?
Thanks
The sort of data that you describe is ephemeral in nature, meaning that it ( status in your case) is transitory / short-term; and it is not recommended to have such data stored in a database. It is okay to have it as a part of your schema, but I wouldn't recommend updating the database, using a cron job.
What you could do instead is modify your middleware function tied to your API endpoint route, which serves the status information (along with other user information, of course) to calculate it for you and then send the response along.
Assume we have set that endpoint route to be handled by a middleware function called fetchUserData... among other things, this is how I would write it to send the status information.
exports.fetchUserData = function (req, res) {
User
.findById(req.body.userId)
...
.exec(function (err, user) {
...
// Logic for calculating whether "now" falls somewhere
// in between the user's "online" schedule
? user.status = 'online'
: user.status = 'offline'
res.json({ user: user })
})
}
I would just use integers for the workdays -> 0-6 for the weekdays and online would be a boolean so i don't have any natural language semantics in the database.
as for the change I would do that in the application which would be a simple logic there was a beautiful java solution for this kind of problem
hashnode.com/post/need-explanation-on-shift-and-b…
but not everyone likes bit operations.
but Usually every date function can return you the day numbered by from 0-7 depends on your logic as well as the date.
docs.mongodb.com/manual/reference/operator/aggreg…
or you could use these methods and just create a shell script that triggeres every minute (because that's basically is the persistent interval) to update all users who should be offline or online.
but maybe I misunderstand the problem.
I have a small lib named bella-scheduler that may help you by writing as below:
scheduler.every('sun 15:30:00', () => {
// do what you want on Sunday at 15:30
});
Feel free to create issue if it's not enough flexible for you: https://github.com/ndaidong/bella-scheduler
Sandeep Panda
co-founder, Hashnode
Hi @argjendhaxhiu ,
I would recommend what @saiki mentioned. But this method won't work well if you need to query and list all "online" users in the system at any point. If this is a necessity, I suggest you use something like node-schedule to run a cron job every 30s or 1m and insert online users into a separate collection, say
onlineusersand remove the offline ones from the same. However, if this is not a requirement, go with the approach mentioned by @saiki .