N
N
Nick932016-04-05 17:48:03
MongoDB
Nick93, 2016-04-05 17:48:03

How to properly use a database for chat?

Hello everyone, I wanted to write a chat on go in my free time. How to properly use a database when writing a chat? There will be only a mobile application, I will use tcp.
And is mongodb suitable for this if there are connections, namely, there will be rooms that are associated with geology, and rooms are associated with users?
As for saving to the database, as I understand it, after sending a message by a user, it will be necessary to save the message to the database, and after saving, send a push that the message has been saved and transfer it to other users connected to the room? And do I need to wait for saving to the database? For example, save asynchronously, and send the message that came via tcp to other users of the chat? And then only load messages from the database as users watch the message history? I would like to clarify exactly the messaging process itself, since I do not fully understand how it all happens. (I am an ios developer myself and in my free time, I want to try to learn backend development.)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
U
uvelichitel, 2016-04-07
@Nick93

MongoDB document base. Starting a new document for every 'hello' and smiley looks like waste, so I vote for Postgre.
Now in chat building it is fashionable to structure messages in a room with a tree of topics, and not pour them out in a continuous stream.
In the trend, the architecture is "one source of truth, one way to change the state", that is, all changes are immediately written to the database. This is considered more consistent. And it’s good to implement the pubsub mechanism in some form, so that you can subscribe to state changes of interest (rooms / topics). When writing to the database, subscribers are sent a notification that the state has changed and needs to be updated with a request to the database. However, you can update from time to time without notification without pubsub and without push.
The subscriber makes such a request, updates the entire state, and the task of the client side is to make a diff / patch so that the view is not completely redrawn, but only in the necessary places.

A
Alexander Pavlyuk, 2016-04-05
@pav5000

If you need connections, then it is better to take PostgreSQL, for example, instead of Mongi. There are also geolocation features, and you can even work with JSON natively.

K
KkJ, 2016-04-08
@KkJ

Disk database for chat? Are you seriously?
In-memory database - Tarantool.
Able to save its state to disk.

M
mukizu, 2016-04-19
@mukizu

From databases, you can also look at https://www.rethinkdb.com/ .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question