Before reading this: This is not a question about "Is SignalR better than REST?"
Imagine creating a new API project using .Net 5 or 6 I see many projects providing a REST API to
GETPOSTand SignalR hubs to notifiy the clients about changes in realtime.
Based on the docs sample for SignalR
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
clients can call actions on the server that might write data to a database. So I could replace the REST POST endpoints with hub actions.
Based on the docs sample for communication
I could also ask for data, e.g.
public Task CallerRequestedAllMessages()
{
Message[] allMessages = database.ReadAllMessages(); // pseudo code
return Clients.Caller.SendAsync("ReceiveMessages", allMessages);
}
and replace the REST GET requests.
Are there any technical reasons to keep the REST API? Of course I can't consume the API directly like so
curl -X GET --header 'Accept: application/json' 'https://my-api.com/messages'
but that shouldn't matter when creating a "real" client connecting to the hubs and it seems people are working on packages for autogenerated SignalR API documentations.
Is there anything a .Net 6 REST API project provides or solves that a SignalR project can't?
It really depends.
Rest is between client and server.
SiglaR is between client and server PLUS (optionally) for other clients as well.
Another thing in Signalr is the ability to "push" data to client.Rest can't do that.
Another consideration is the battery. keeping a socket open takes more battery. Another consideration is the strength of the server in terms of concurrent connections limitations.
I do see the benefit of using them both depending on the scenario.
In a low reception signal env i'd use Rest. not SignalR.
In a low battery mode , I'd use Rest.not SignalR
In a one to one only communication ( client to server only) , I'd use Rest , not signalR.