I am currently developing a semi-simple chat application. I want the user to be able to chat with another person in a private chat. Only plain text will be sent. Currently, my system calls php scripts from a web page, passes parameters and then analyzes the returned data. I have it so that the client sends a message that causes the script to send a message on my web server, the script then makes 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, he would have to call a script that checks the server for the message file with the message for him. If he finds one, he sends a message back; if not, he sends a response about the absence of messages.
This method works great, except that the client will need to manually update to check if it has messages, or the background thread should be updated every few seconds. That would be nice, but it would use the data if the user was on a mobile network. Not to mention what resources the background loop will pull if it were refreshing at a speed that would be comfortable.
So, I decided the second idea, it will be a server programmed in Java, which will communicate through sockets. The user sent a message in the form of a packet through a socket, and the server will check to whom it is intended. If a person is online, he sends a message to this user. However, this method requires constant communication between the client and the server, and this is unreliable, because if the user is in the car and the data is cut out. Or some other situation when the connection is interrupted. It will then cause errors and must reconnect.
Anyway, my question is which technique is better. Or are they both terrible? If so, what is the correct way to do this? Thanks in advance.
source
share