'C# WebApi + React Native Chat

I am writing a ReactNative application with a C# Rest Api server. The application communicates via POST and GET requests (transfer of database records, user profiles, etc.). Now there is a need to create an internal chat in this application. I send messages from the client with POST requests, in which I pass the client token, the message text and the user name to which the message is sent (everything is encrypted). But there was a problem. As a chat user, receive new messages instantly. The first thing that came to mind was to also send a request to the server every 100 milliseconds to receive new messages. If there are messages, the server will return them to the client, if not, it will return an empty response. But I understand that this is some kind of crutch method and there may be a big load on the server. I started reading about SignalR and hubs. But I didn't find high-quality information. As far as I understand, messages will be delivered only if the second user is also connected to the hub. What if it's offline? Then the messages will not be delivered to him? I still don't understand in which direction I should work and which path to choose. Maybe this SignalR is not required at all. Which way should I choose? What kind of management architecture is needed to create such a chat between users inside the application? I hope for your understanding and desire to help the beginner).If possible, provide an example of the server (C#) and client (ReactNative js) code. Thank u.

    [AcceptVerbs("POST")]
    [Route("GetNewMessages")]
    public List<BDMMessenge> GetNewMessages([FromBody] string token)
    {
        string user = db.bdmuser.Where(x => x.Token == token).Single().Email;
        var messages = db.bdmmessenge.Where(x => x.ToUser == user && x.ClientGet == 0).ToList();
        SetGetStatus(messages.Select(x => x.Id).ToList());
        return messages;
    }


    [AcceptVerbs("POST")]
    [Route("SendTextMessage")]
    public DateTime SendTextMessage([FromBody] MMessageText _message)
    {
        string user = db.bdmuser.Where(x => x.Token == _message.Token).Single().Email;
        var dataStatus = DateTimeOffset.Now.UtcDateTime;
        db.bdmmessenge.Add(new BDMMessenge()
        {
            FromUser = user,
            ToUser = _message.ToUser,
            Type = "Text",
            Content = _message.Content,
            DataStatus = dataStatus,
            ClientGet = 0
        });
        db.SaveChanges();
        return dataStatus;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source