MongoDB & SMTP: What's the most efficient way to email every user - node.js

I have a Node.js & Mongoose App and I need to email every user in the MongoDB Database about a new Privacy Policy Change. I have a total of 10.000 users. What would be the most efficient & fastest way to send every user a email? I thought of a service like Postmark. Would that be a considerable option? How does Google do it to send so many emails in a short time?
My current approach (in Mongoose) would be this:
const users = await User.find({})
for (let user of users) {
// send user a email using e.g. Nodemailer...
}

Related

How do i delete chats from specific user in MERN stack?

i am creating a website where user can chat with different users so now i am trying to make a delete chat button so that user can delete chat with any chat. i also built one delete account button but that's working fine as it uses deleteone query. but i am not able to use the deletemany option for the above problem.its deleting entire message database
this is the message controller
"to" used with the deletemany is the receiver id(user[1])
module.exports.deletemessage=async (req,res,next)=>{
try{
await messagemodel.deleteMany({ to: req.params.to});
}catch(ex){
next(ex)
}
}
This is the message model:
so I tried using "to" as its the only unique id of all chats with a specific user.
also its working and giving no errors but its deleting all the chats in the database.

Is there something I can use to uniquely identify a device connecting to a socket with Socket.IO?

I'm working on an app that keeps users together in a room using socket.io. While using the app, I'm keeping track of actions the users take. If a user disconnects accidentally (in my use case, their phone rings, or the screen shuts off), I want them to be able to re-enter the room as the same 'user' without requiring a login so the actions they've tracked stay with them. I tried using socket.conn.remoteAddress, but that doesn't seem to be consistent enough to rely on.
For now, I'm requiring the user to manually enter a username and match to the user with that name on the server, but I'd rather it be automatic and invisible to the user, not to mention more reliable than what each user inputs.
Use a cookie. When they connect, check if their unique cookie already exists. If not, create it with a unique ID in it. If it does already exist, use the unique ID in it to identify the user.
From the connect event in socket.io, you can get the cookies here.
const socketCookieName = "socketUser";
const cookieParser = require('socket.io-cookie-parser');
io.use(cookieParser());
io.on('connection', function(socket) {
// all parsed cookies in socket.request.cookies
let user = socket.request.cookies[socketCookieName];
if (!user) {
// create unique userID and set it in a cookie
user = /* create some unique userID here */;
// set this into a cookie
}
// now user will be your socket.io userID
});

How to create multiple instances of nodejs server

I am building a notification application in which every time an event is triggered the associated user gets a notification. I am using localstorage of NodeJs to store the information of the logged in user. The problem is that when two users are logged in,the localstorage values are overridden by the new users value.
I need to create multiple instances of NodeJS server so that every user has its own localStorage.
For example
If two users log in with credentials
{
userName:name1
}
and
{
userName:name2
}
then two separate localStorage should be created one having userName:name1 and one having userName:name2.
I have tried all the solutions available online but I am unable to create multiple states of NodeJS server.
Thanks in advance
You do not have to create a new server for each user. Take the IP address and port instead. This means that each user can be uniquely identified. You can simply name the files of the users after the variable client.
Here an example code
net.createServer((netSocket : net.Socket) => {
netSocket.on('data', (data) => {
var client = netSocket.remoteAddress + ':' + netSocket.remotePort;
})
})
I wasn't able to create multiple nodejs instances hence I stored the user data in session storage and passed it to nodejs server every time I triggered a request.

ExpressJS prevent my app from being hacked

I am writing an app that will give user1's money to user2, if user1 agrees that user2 completed a task successfully.
I have several ideas on how to go about this, but am worried about security. Most likely I am not understanding this subject as much as I should and am hoping for advice.
One method is to listen if user1 is happy with user2's job completion.
Example Express code saving data to a Firebase DB:
app.post('/general/something', function(serverReq, serverRes) {
var data = serverReq.body;
var ref = db.ref("stuff/"+data.userOneId).update({
isHappy: true
});
serverRes.send("Posted");
});
Then I could check if the user is happy in the DB and then send user2 user1's money.
However, it seems as if anyone (especially user2) could just POST DATA (user1's ID) to my server at "/general/something" and receive userone's money.
We can assume that user2 knows user1's Firebase userID. This is because I need the users to be able to reference each other and an userID is the only way that I've found with Firebase (without giving the other user's email address out).
What is the best way of completing this task?
Thanks to #ElanHamburger I am able to fix the problem using tokens with Firebase (https://firebase.google.com/docs/auth/admin/verify-id-tokens).

Sending data to a independent remote server

I want to send a json object to another server, independent of my website.
The API I am using requires a user (user x in this case) to log into their service to be authorized so user x can manipulate user x's list. However, other users can't write to x's list. So, users need to request an item to be added to x's list, then a server who is logged into x's account can add it to x's list. Refer to the image below.
http://imgur.com/a/wT53t
I am using node/express.js for the servers on the user's side. However, I don't know what I should use for a server who's only job is to receive requests and write to x's list. Can you provide some guidance as to how I can achieve something like this?
Thanks!
There are two options here:
You have to refresh the list in the realtime for connected users.
To achieve this you should use either: WebSockets(e.g. socket.io) or LongPolling.
In second option you dont have to refresh the list in the realtime. You simply use express. Accept data and refresh the list server-side.
Auth with web sockets:
Once understanding the nature of web sockets, you're free to build any logic around them, including authentication/authorization. The great library doing lots of auth things is passport.js.
Very quick and abstract example of server-side:
socket.on('auth', function(data) {
const vendor = data.vendor,
token = data.id
switch(vendor) {
/*
Here you grab through some social API user social id
and save it to database
*/
}
/* set socket as logged in */
socket.logged = true
/* or even save user object */
socket.user = { vendor, token }
})
Next time you need authorized user, you check:
socket.on('mustBeAuthorized', function() {
if(socket.logged || socket.user) {
/* Continue your logic*/
}
})

Resources