I am implementing a rather simple communicator - one broadcast chat and multiple others for private messaging between users/clients. The purpose of this app is educational and I want to use solutions that are (preferably:)) simple yet modern, flexible, and used in real life, I started implementation using RestController and I stored clients' URLs in the database but I quickly understood that this is not good practice. I want to ask about links and resources that may address the following questions:
- How to make a flexible and secure connection between server and client without storing the latter URL?
- What is the correct protocol for information exchange in this kind of application
CodePudding user response:
One way to make a flexible and secure connection between a server and its clients without storing their URLs is to use WebSockets. WebSockets is a protocol that allows for full-duplex communication between a server and its clients. This means that the server and the clients can both send and receive messages at the same time, and the connection remains open until it is closed by one of the parties.
Using WebSockets, the server can send messages to specific clients or broadcast messages to all connected clients. This allows for both private messaging and broadcast chat functionality in your application.
You can use a WebSockets library such as Socket.io for Java (https://github.com/socketio/socket.io-client-java). This library makes it easy to set up WebSockets on the server and handle incoming and outgoing messages from clients.
Some resources that may be helpful for learning more about WebSockets and implementing:
- The WebSockets API: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
- Socket.io: https://socket.io/
- A tutorial on using Socket.io for real-time communication: https://socket.io/get-started/chat/