I need to implement some push notfications for my asp.net core 2.2 site. I found signalr, and started looking at it. I have tried implementing a small toy example, and I now have a hub class that looks as so:
As simple as can be. I now have a controller from which I need to trigger a push notification on the client computer. which could be triggered by thus controller being hit (In my real project, I need to send a push notification when a user is online, and when a new article is published on my site). So here is my controller:
public class msgController : Controller
{
public IActionResult Index()
{
return Content("serving content");
}
}
But I now need to somehow trigger my hub's sendMessage function. I'm able to call it if I just put the hub inside the controller, like this:
public class msgController : Controller
{
private IHubContext<NotificationHub> _hub;
public msgController(IHubContext<NotificationHub> hub)
{
_hub = hub;
}
public IActionResult Index()
{
_hub.Clients.All.SendAsync("ReceiveMessage", "bruger", "besked");
return Content("serving content");
}
}
But I would like to use my actual Hub class, and also, none of the docs that I can find do it like this. How could I do this?
I've seen some places giving answers that does'nt really make a lot of sense to me, like just accessing the contexthub, through a GlobalHost, that is'nt available to me. Other places claim that this just is'nt possible.
So how do I call/trigger my hub, and send a message to the client? Also, the version of my site where the hub is just written in the controller works now, and a sendmesage() function is called. But I can't really see the message being sent clientside. Where can I find it?