Reputation: 1122
I am developing a Web-app with Angular (client-side) and Node (server-side). I'd like to integrate some social features, including a chat. So a user can discover near users and send them a message. I want to know the best way to implement this. I have an idea, but it seems to me very raw, and I am afraid that it could overload the server.
my idea is to send each minute from the client, a request to the server about new messages
the server looks for new messages among all the conversations of that user, checking the last-message's time for each conversation
the server sends back the conversations with new messages
if the client receives conversations with new messages, a notification appears, so the user can open the chat.
once the chat is opened, the request to the server for new messages is sent each 3 seconds (instead of 1 minute)
Example of conversations of a user, stored in MongoDB
{'conversations':
[
{'to':{'user-id':'101010', 'name':'Michela', 'location':'Alba Adriatica',
'img':'http://graph.facebook.com...jpg'
},
'last-msg':12345, //epoch
'msgs':[
{'from-me':'ciao come stai?', 'date':''},
{'from-you':'bene grazie, tu?', 'date':''},
{'from-me':'eh insomma..mi so rott lu cazz', 'date':''},
{'from-you':'dai poi vai alle Hawaii', 'date':''}
]
},
...
]
}
Upvotes: 2
Views: 192
Reputation: 1638
You realy should learn about websockets and how they can provide 'push and pull' of information.
the approach your suggesting called 'long polling'. And yes, if the time between is not very long, the server will be under heavy load if the number of clients increases.
Using websockets you can let the server communicatie only with the clients that actually need the information.
Do a google search for "node.js chat application tutorial". It is not that difficult.
Upvotes: 1