I am working on a Java application (JSP/Servlets) where users can send messages to each other. For this purpose I am using a single table in MySQL with fields such as msg_id
, sender_id
, receiver_id
, msg_content
etc. When user_A sends a message to user_B I want to pop a notification to user_B that indicates that he has a new message (Something like how Facebook notifications work).
I've searched for a solution to this and found out that a possible way to implement it is by AJAX polling. That means that every 60 seconds for example the application will check the table mentioned above for new rows (messages to user_B) and if new messages are found then it will pop the message.
However, lets say that 100 users are connected. This means that for every single user there is going to be a request to the database every 60 seconds, which sounds pretty bad.
Won't the application get really "heavy" when something like the example above happens? What are other alternatives to achieve this?
For this case I would recommend using sockets with some message broker, for example RabbitMq, but there are lot others. Instead of repeatedly checking status you create message queries in message broker and server as same as all clients are connected to that message broker via sockets. When new message arises, you send it to all corresponding message queries. Corresponding clients connected to corresponding queries will get the message over the socket immediately.
If i am not mistaken, for web applications you can use websocket to create connection with your browser. That is what facebook uses for his notifications.
For client-side (javascript) implementation and good explanation, see this link. There are also some server-side examples, but not for PHP (for example Node.js or python)
For server-side in PHP on this link you can find complete PHP implementation (but not so good explanation).