Java – How does a chat application communicate with its server?

How does a chat application communicate with its server?… here is a solution to the problem.

How does a chat application communicate with its server?

I am currently working on a semi-simple chat app. I want users to be able to communicate with other people in private chats. Only plain text is sent. Currently my system calls PHP scripts from web pages, passes in parameters and parses the returned data. I have it so that the client sends a message, it calls the send message script on my web server, and then the script creates a message file on the web server and then returns success or failure to the client. Then, in order for the client to view this message, it must call a script to check whether the server has a message file that contains the message addressed to him. If it finds one, it sends a message back, and if not, it sends a response about no message.

This technique works very well except that the client has to manually refresh to check if he has a message, or the background thread has to refresh every few seconds. That would be nice, but if the user is on a mobile network, that will use the data. Not to mention what kind of resources the background loop would fetch if refreshed at a convenient speed.

So I decided on a second idea, which would be a server programmed in Java that would communicate over sockets. The user will send the message as a packet over a socket, and the server will check who it is going to send to. If the person is online, it delivers the message to the user. However, this approach requires a constant connection between the client and server, which is unreliable because of what happens if the user is in the car and the data is interrupted. Or some other situation where the connection is cut off. Then it throws an error and has to reconnect.

Anyway, my question is which technique is better. Or are they all terrible? If so, what is the right way to do so? Thanks in advance.

Solution

AngularJs and Ajax will be the perfect solution for you, try to learn

Use AngularJs for true real-time messaging

Related Problems and Solutions