I am building a chat app using Node.js, Express, React and Socket.io. I want to keep track of unread messages so I can notify the user with unread messages count. Should this be implemented on both server and client sides?
Check this to see some of the tradeoff doing it either on client or server side.
Also it depends on how you are implementing it.
I'm currently doing something very similar and I chose to keep track of the last time the user checked the chatroom/ conversation and compare it to the created_at date of the messages. So the server sends off all the chatrooms and their last 50 messages to the client. I then calculate the number of unread messages directly in redux via a helper function and display it on the chatroom badge.
Related
I am trying to implement private messaging with socket.io for my mobile applications which have a direct messaging feature like Instagram. Right now, I am using Node.js and React Native. I am kinda new to socket.io. I saw many examples of that. However, one thing is not clear in my mind.
User clicks "send message" button. Then I create a socket connection and the user joins a room with socket id. Then user sends a message to that room.
The problem here is, how other user will get the message? Because at this point, I don't think other user knows the room id. Of course if there is a better solution for that, I am open to every suggestion.
One thing you can do is create a room for each person. When the person logs into your app and connects with socket.io, you'll want to have them automatically join the room with their user id.
Then when someone wants to send them a message, they can just send the message to the room for the receiving user.
However, I think if you are building a messaging app, socket.io is not the right way to go. As far as I know you can't listen on sockets while the app is in the background (and even if you could, it would drain your users' battery life). You should use push notifications instead and use the data field (e.g. zo0r/react-native-push-notification and firebase).
I am creating an chat application where I have a rest API and a socket.io server, What I want to do is the user will send messages to rest API, The api will persist those messages in database and then send these messages to the rabbimq queue then rabbitmq will send these messages to socket.io if the receiving user is online, Else the message will be stored in the queue and when the user will come online the user will retrieve these messages from the queue however I want to implement this in a way like whatsapp, The messages will be for a particular user and the user will only receive those messages which are meant for them i.e I don't want to broadcast messages I want only particular user to receive those messages
Chat should be a near-real-time application, there are multiple ways of modeling such a thing. First of all, you can use HTTP pooling, HTTP long pooling but some time ago there where introduced the new application-level protocol - web socket. It would be good for you, along with Stomp messages. Here you can check a brief example. And sending messages to specific users is also supported out-of-the-box example
1
To send messages to specific sockets you can use rooms: io.to(room).emit(msg). Every socket is a part of a room with the same name as the socket id.
2
I wouldn't wait for the message to be written to the database before sending it out through socket.io, your API can do both at once. When a user connects they can retrieve their messages from the database then listen for new ones on their socket.
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':''}
]
},
...
]
}
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.
I'm attempting to create an application which will work as a chat app. I'm currently contemplating the best way to do this and I'm thinking of going with a server sent event package such as the following. Every conversation would have an id, and the message would be emitted under the id. For instance
stream.emit(1512, "Hello") would send the message and
stream.on(1512, function(message){console.log(message)}) would print the message. Only the chat members would have the chatId.
I was initially thinking of using websockets but I thought that not every user should be receiving data, as chats were private and I didn't want to configure authentication within websockets.
Back to server sent events:
I have a few questions on the topic.
Are they efficient and, if not, what would be a more efficient solution
Is the method of sending chat through a randomized, hashed, id (such as 309ECC489C12D6EB4CC40F50C902F2B4D) secure?
Would you recommend a different method for sending chat? This is to be implemented as a mobile application where individual users can chat privately with oneanother so, again, security is pretty important.
Thanks.
I recommend the client-call package (disclaimer: I wrote it). It provides a very simple method to run a client-side method from the server code.
Besides this, you can always just put the chat messages to a db collection and remove them after some time.
I'm very new to node.js and sokcet.io that's why I need to ask you about the plan I have to see if it makes sense or is plain stupid. I need ongoing server/client communication for two reasons: sending real-time notifications to the user when they have one, and two, for chat between users.
Here is my plan for managing notifications:
PHP script finds out user X has a new notification.
Using Elephant.io send a message to server with user X's id as the data.
On the server side, upon receiving the message, if user X is connected emit him a message telling they have a notification.
user X's brower, Upon recieving the message, uses AJAX to poll the database and receive the text for the notification.
For chat, this is my plan (messages should be save on DB):
When user X submits a chat message to user Y, use ajax to send the text to a PHP script and save it on DB. On success, use elephant.io to send a message to user Y telling them that they have a new chat message.
User Y's browser, after receiving the server message, uses AJAX to poll a php script to receive the new text.
Do you think these plans are superior to short polling using AJAX? I appreciate any comments to improve them.
Finally,I'm curious to know how reliable these technologies (node.js, socket.io, elephant.io) are. Do they work well when the server becomes busy? How do they handle exceptions and errors ,etc.