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

What is a real-time message to build an instant messaging application

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is real-time message to build an instant messaging application". The content in this article is simple and clear, and it is easy to learn and understand. let's go deep into the editor's train of thought. Let's study and learn "what is real-time messaging to build an instant messaging application"?

Message client

Before the HTTP section, let's write a mapping map so that all clients listen for messages. Global initialization like this:

New messages created by type MessageClient struct {Messages chan Message UserID string} var messageClients sync.Map

Remember in the previous article, when we created this message, we left a "TODO" comment. From there, we will use this function to schedule a goroutine.

Go messageCreated (message)

Insert this line of code where we left a comment.

Func messageCreated (message Message) error {if err: = db.QueryRow (`SELECT user_id FROM participants WHERE user_id! = $1 and conversation_id = $2`, message.UserID, message.ConversationID). Scan (& message.ReceiverID); err! = nil {return err} go broadcastMessage (message) return nil} func broadcastMessage (message Message) {messageClients.Range (func (key, _ interface {}) bool {client: = key. (* MessageClient) if client.UserID = = message.ReceiverID {client.Messages

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