Search code examples
javac#socketsnetwork-programmingtcp

Is maintaining a socket connection to a server-side service bad for sending notifications to desktop applications?


I'm currently working on a semi-small spare time project to learn some new technologies. My plan is to create a chat application, client-side being built in WPF, and server-side with Java.

What I'm curious about is what approach I should take to sending and receiving messages. My current idea is to log the user in using a socket connection, and then maintain that connection to send that user notifications when he/she receives a message. Many people might have an application such as this run for many hours on their computer, so is maintaining a socket connection bad? If so, is there another approach I should consider instead?


Solution

  • TCP socket connections can be left open for a long time, but will break eventually, for example when the network interfaces on either side of the connection are reconfigured, or when the server or client side software is restarted.

    To detect breakage, some protocols include a "heartbeat" mechanism. The client and server regularly exchange a message that says "I'm still here, are you there?" If the client doesn't receive a heartbeat from the server for a while, it can assume that the connection has been broken and try to reconnect.