Maurice Noel Bouniol
Maurice Noel Bouniol

Reputation: 43

Chat Application, how to store chat history?

I'm developing a chat application in Java.

The architecture used is Server - Client(s) architecture.
The majority of the code is in Java, JavaFX for the GUI and PostgreSQL as the Database.

As this is a chat application (desktop), I'd like to know which is the best way to store chat history:

  1. Locally in a text file, that the client has to read every-time
  2. In the database as of type String (VarChar)
  3. In the server as Lists

Some questions based on the three ways:

  1. If a client connects from a different machine the text file will not be there
  2. Is it possible to store every text entry in the database with a chatroomID?
  3. How many objects can be stored in the server for as long as it runs?

Upvotes: 4

Views: 9295

Answers (3)

Retro Gamer
Retro Gamer

Reputation: 1112

Out of your three choices, I recommend you choose option #2 for storing chat history: A database, and here is why:

  1. If you store the chat history locally in a text file, you run into issues such as how to sync with others. Also, you can modify the contents of the text file without going through your Java program (such as with an editor). If this file contains chats with sensitive information and someone has access to your computer, they can read it. This spells trouble.
  2. Storing in a database is a great idea because it provides a central location for all your Java program. This is especially handy if multiple people are using your Java client, that way they can fetch chat history, as well as easily transfer chats to others! I wouldn't only use a type String (VarChar), but try to think of some other useful fields or columns that might be useful (i.e. timeSent, chatUserID, timeRead, etc). This also brings up the point that by using a database, you are able to set up some sort of user access rights (username and password) so that specific people can read specific chats.
  3. If you store the chats on the server as a list within the Java server itself, and if your server restarts, you lose all your chat history. Bummer.

To sum up, keeping your Java client-server-database architecture is perfectly fine, and technically all 3 options could work, but databases is the way to go for storing your chat history! Even if setting up the database takes a little bit of work, it proves to be superior in efficiency and security out of the other 2 methods described since databases are built for archiving data.

Upvotes: 2

Angel
Angel

Reputation: 432

I'm in a similar situation, I'm also developing a chat from 0, the only difference is that I'm doing it for iOS.

The way I'm developing my chat is:

  1. I use an Ubuntu web server.

  2. The server has a database in Mysql and the communication with the user is done through NodeJS.

  3. In NodeJS I have a socket "Socket.io" which facilitates notifications between users.
  4. On the iOS device, I store all the messages that it receives in Core Data, which is an extension of SQLite.
  5. In order to obtain the pending messages depending on the device where the user is connected and I use an ID for each device, this ID is created and identified by the MAC physical address and thus what messages to obtain from the server and what not.

Initially I base myself on this database to know how to structure my application: https://github.com/yoosuf/Messenger

Socket.IO is incredibly easy to use and the best thing is that it has libraries for different programming languages, here is its page: https://socket.io/ https://github.com/socketio/socket.io

Upvotes: 2

Julian Mayer
Julian Mayer

Reputation: 1

I would say Nr2 - for safety reasons (if you care about it) and because it is a pretty easy way. For the beginning a database with 4 columns should be enough (Date/Time, ChatroomID, UserID or just a name and the message itself). If a user sends a new message it creates a new row containing all the information that is needed for the columns. You can easily iterate through it as well when your client reloads (maybe every 10 seconds)

Upvotes: 0

Related Questions