AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

How to send system messages

Open nelfer opened this issue 8 years ago • 1 comments

Hi, I followed the examples and they work great. But the message are always initiated by the page and sent to all the clients.

How would I go about sending to all the clients a "system message"? I tried instantiating the ChatHub class but got null reference when trying to send (probably because it's not the instance that is running)

So, what I'm trying to do, all of the clients that are connected on the chat get a message "System says get out" (as an example). Let's assume a system administrator presses a button to send this message to all connected users.

How can I accomplish that? Note: I know that I could create the button to post a message to the hub from the "admin" page. But I'm more interested in how to do it from the server side. For example when some process completes in the backend.

For reference, here's the stack call:

SignalRChat.Hubs.ChatHub.SendMessage(string _user, string _msg) in ChatHub.cs

    		public async Task SendMessage(string _user, string _msg) => await Clients.All.SendAsync("ReceiveMessage", _user, _msg);

SignalRChat.Pages.AboutModel.OnPostAsync() in About.cshtml.cs

    			await sender.SendMessage("System","Get out");

Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+GenericTaskHandlerMethod.Convert<T>(object taskAsObject)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+GenericTaskHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

nelfer avatar Aug 28 '18 01:08 nelfer

Oh never mind!

I found the page after doing a quick search

https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.1

And it works!!

nelfer avatar Aug 28 '18 02:08 nelfer