V
V
Vitaliy2020-09-09 23:57:20
Java
Vitaliy, 2020-09-09 23:57:20

How to implement communication between Java clients?

Hello, I'm writing chat in Java. There was a problem in the implementation of sending / receiving messages by clients.
If 2 clients: client1 and client2.
1) client1 sends a message to the server for client2.
2) The server receives the message and saves it to the database
3) The server should send a message to client2 and the message should be displayed in the client's GUI.
That is, the server must send a message to a specific thread.
How to implement it?

ps. On the server I have 1 thread = 1 client.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2020-09-10
@leahch

- very bad tactic: 1 thread - 1 client! Use connection pools, or libraries like netty.
- the exchange can also be done via http / https, for this it is enough to use some kind of server like tomcat or jetty or undertow ....
- you can use libraries to build servers and services like spring boot, javalin and others like them
- you can use for example grps
- you can exchange json or protobuf or avro over any protocol
- you can use message buses: actvemq, kafka, pulsar, redis ....
- you can spit on everything and take newfangled mosquitto and mqtt
- you can work via websockets
I would start with a simple http + json or grpc or mqtt... In severe cases kafka or pulsar
I would not take websockets, you are tormented with the client part !!!
My favorites
are http + json - https://javalin.io/ + https://github.com/google/gson
- https://grpc.io/
- https://pulsar.apache.org/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question