Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 23:27:07 10月02日 Update

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

Tags: Messages clients real-time applications formats channels loops events functions data servers learning services content that is more comments articles and will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Information Shulou Technology Apple