Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build an instant messaging application with go and javascript

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "go and javascript how to build an instant messaging application", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to build an instant messaging application with go and javascript".

We will use CockroachDB as the SQL database, Go as the back-end language, and JavaScript to make web applications.

CREATE TABLE users (id SERIAL NOT NULL PRIMARY KEY, username STRING NOT NULL UNIQUE, avatar_url STRING, github_id INT NOT NULL UNIQUE)

Obviously, this application needs some users. We take the form of social login here. Because I chose GitHub, I need to save a reference to the GitHub user ID.

CREATE TABLE conversations (id SERIAL NOT NULL PRIMARY KEY, last_message_id INT, INDEX (last_message_id DESC))

Each conversation refers to the most recent message. We update this field every time we enter a new message. I will add foreign key constraints later.

... You might think that we can group the conversation first, and then get the latest news in this way. But doing so makes the query more complex.

CREATE TABLE participants (user_id INT NOT NULL REFERENCES users ON DELETE CASCADE, conversation_id INT NOT NULL REFERENCES conversations ON DELETE CASCADE, messages_read_at TIMESTAMPTZ NOT NULL DEFAULT now (), PRIMARY KEY (user_id, conversation_id)

Although I mentioned earlier that the conversation will only take place between two users, we adopted a design that allows multiple participants to be added to the conversation. Therefore, there is a participant table between the conversation and the user.

To know if the user has an unread message, we added a messages_read_at field to the message table. Whenever a user reads a message in a conversation, we update its value so that we can compare it with the created_at field of the message at the end of the conversation.

CREATE TABLE messages (id SERIAL NOT NULL PRIMARY KEY, content STRING NOT NULL, user_id INT NOT NULL REFERENCES users ON DELETE CASCADE, conversation_id INT NOT NULL REFERENCES conversations ON DELETE CASCADE, created_at TIMESTAMPTZ NOT NULL DEFAULT now (), INDEX (created_at DESC))

Although we put the message table at the end, it is quite important in the application. We use it to hold references to the user who created it and the conversations it appears. You can also create an index based on the creation time (created_at) to complete the sorting of messages.

ALTER TABLE conversationsADD CONSTRAINT fk_last_message_id_ref_messagesFOREIGN KEY (last_message_id) REFERENCES messages ON DELETE SET NULL

I already mentioned this foreign key constraint earlier, didn't I: d

These four forms are enough. You can also save these queries to a file and pipe them to Cockroach CLI.

First, we need to start a new node:

Cockroach start-insecure-host 127.0.0.1

Then create the database and these tables:

Cockroach sql-insecure-e "CREATE DATABASE messenger" cat schema.sql | cockroach sql-- insecure-d messenger above is about "how to build an instant messaging application with go and javascript". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report