Java chat socket milion clients - java

I would like to know the architecture of real time chat for millions of users like whatsapp.
I implemented a chat but I need to use more servers to handle more clients
I read another question, but I still have doubts about.
Tell me if what I say is correct:
Clients connect to load balancer that decide to give connection to one of the whatsapp servers.
Example ClientA connect to Server1,
ClientB connect to Server2,
-If ClientA wants to communicate with ClientB, sends a message to the Server1 ,now, this how do you know in which server is ClientB ? Maintaining the scalability to millions of users )
How to implement a chat to handle millions of users?

The key is the database (or any other persistence system) where all the input from the clients gets stored. Usually in these scales, a key-value NoSql Database is suitable, like Apache Cassandra, Amazon Dynamo, or Google Datastore.
These databases are optimal for fast insert and fetch by PK only.
All the servers need to be stateless, so Client A connects to Server A and sends a text message and destination client Id. This gets stored in the DB. Then, client B connets to Server B which goes to the database and retrieves the text.

Related

Secure Messenger in Java

While creating a Secure Instant messenger in java , if there are many clients
for example
3 clients
client A,B,C
A wants to connect to B and C both. So do i need to create different socket connection for both of them separately?
If so than is not it is a restriction like if there are 10,000 clients and each wants to connect to rest of them so i will need million ports?
Yes, your solution would work. But when you add many more users, the instant messager will lag and be hard to maintain.
The better solution is to have one connection for each client, a connection to a server. clients will connect to this server (which will also handle authentication), and the server will sort out the messages and deliver them to another client(the messages destination).

how to make a socket server for multiple clients

I have seen a lot of tutorials on client/server chat rooms using sockets, I am trying to create a instant messenger which will allow users (stored in a sql db) to chat with there contacts and groups(also stored on sql db). now I am really puzzled where to start. how would i go about creating a server which can handle peer to peer chat and group chat. I am using a mysql database which will store the user data and contacts list.
To get you started on ServerSocket and ClientSockets for multiple clients you can refer to the below post.
Two Socket sharing a port
Ideally every client would have only 1 socket connection to the server. To differentiate between your chats you can very simply use a unique identifier which will help differentiate between the different chat types.
You will need to create a multi-threaded socket server, this will accept incoming connections on a loop, and then pass off all operation between that instance of the socket and the client into a separate thread. This will allow you to run multiple client connections at once. This Page goes into great detail about creating both single and multi-threaded chat servers.

How to find instances of active servers on the local network?

I have a spreadsheet application in Java, and one of the features it provides (which I developed) is sheet sharing. Basically, anyone can be a client or a server because the app has both server and client code. The user who is the server creates the share, specifies the IP, and then the share is created and active (best case scenario) with the server listening for clients on its IP and selected port.
At the moment, the client needs to enter the IP and port of the server that's listening in order to connect. The server then creates a new socket for that client and communicates with in on a separate thread, while the server continues listening on another (traditional TCP behavior). This is all working fine.
What I need to develop is auto-discovery, e.g. a client does not need to type in an IP or port, they simply select 'Join a share...' from the menu and then it starts looking for servers. When one is found, it should send its list of active shares on that IP. The user then selects which share to join from the list, and is connected.
However, I have doubts on how to tackle this issue. Should I use broadcast to poll servers, like DHCP does? Or is there an easier way?
What I'd like to implement is:
Client -> polls local network -> finds a server -> server sends active share list to client -> client selects share to join -> connected!
Technically, what you're looking for is active servers that are running your spreadsheet application.
One possibility would be for your server code to send out an "alive" message to the network every so often (say every 15 seconds). Your client code would listen for these "alive" messages, and produce a meaningful list of spreadsheet servers.
Another possibility would to to use the same database engine that you're using to store the spreadsheets to store the IP and port of the connected server code. The client code would just read the database table to get the connections.

Intercommunication between TCP Servers in Java

I have a application in which I am using socket programming , having this(image) scenario.
Where number of clients will try to connect Broadcast server.
Now here I am managing load through LVS(Load balancer). so as a example shown in image, suppose 200 clients will wish to login for broadcast they will be distributed as 100 users on server 1 and another 100 users on server 2.clients will get connected to servers using TCP connection.
Now I am maintaining user information on server side in arraylist which will be stored in heap memory,Now the problem is if client wish to broadcast to all logged in users, but that particular client is logged in server 1. and so client will not be able to broadcast another 100 users from server 2. Because both ther servers are unaware about each others state.
please suggest me to solve this scenario by whatever means you want.
Have the two servers log in to each other and arrange for broadcasts to be forwarded to the other server and then on to that server's clients.

Java - How to send notification from server to client

I am having a server where my java
web application and database
server reside.
Now the clients of my web app are
of two types:
ClientType1
ClientType2
Both can access the database.
ClientType1 stores the data in the database.
ClientType2 retrieves the data from the database.
But the ClientType1 should start
storing the data in the database
when the ClientType2 says
Start.
Similarily ClientType1 should stop storing the data in the database
when the ClientType2 says Stop
===========================================================================
Q1. What are the solutions for this problem?
Here are the approaches I thought of:
Create a table in the database having one column that shows the status Start or Stop and this column's value should be set by ClientType2. ClientType1 will keep sending the query to the database for getting the status from this table and perform operations according to the status.
Apply ServerPush approach by which the server will keep a connection with the ClientType1 alive and will send the request to him whenever it receives the command (Start or Stop) from the ClientType2. Problem with this approach is that the no. of open sockets at the sever will increase as the increase in the no. of ClientType1
You should use a kind of Ajax for this since this abstracts the "server can call client" away.
Choose a library that allows you to keep a single connection open and do multiple things through this connection.
I don't really consider interprocess communication through a database to be a great approach. The typical scenario is that the client registers itself with the server making it eligible for receiving messages using a socket-based mechanism.
The client can then either:
Ask the server to perform a database operation on behalf of it
Request access to the database
If there are that many clients that keeping an open connection will be a problem you could either initiate a new connection every time you need to communicate with the server or let the server offer e.g., some kind of REST API which the server can poll.

Categories