I am completely new to SignalR. I have a ASP.Net net MVC web application (not .Net Core). But the business logic is written in a separate Web Api hosted in a different server. Now I want to call a SignalR Hub residing in above mentioned Web Api server from
my client's Controller class. The reason why I am not directly calling WebApi server from javascript is I want to pass the currently logged UserID to the Hub too. So I believe it is more secure to get the current logged UserID from the Session variable I created
in MVC controller class (at client's side). Could someone help me with the solution? It will be really helpful if you can show me the complete code. Because I am new to the SignalR thing.
As far as I know,if you want to access the server hub in MVC controller, I suggest you could try to use
signalr client library.
Then you could use below codes to call the server hub method.
Server code for a method that has no return value
public class StockTickerHub : Hub
{
public void JoinGroup(string groupName)
{
Groups.Add(Context.ConnectionId, groupName);
}
}
Client hub method:
using (var hubConnection = new HubConnection("http://www.contoso.com/"))
{
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();
stockTickerHubProxy.Invoke("JoinGroup", "SignalRChatRoom");
}
Best Regards,
Brando
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
2 Posts
How to call a SignalR Hub in a Web Api from a MVC Controller class?
Apr 12, 2020 07:50 PM|chamila91|LINK
I am completely new to SignalR. I have a ASP.Net net MVC web application (not .Net Core). But the business logic is written in a separate Web Api hosted in a different server. Now I want to call a SignalR Hub residing in above mentioned Web Api server from my client's Controller class. The reason why I am not directly calling WebApi server from javascript is I want to pass the currently logged UserID to the Hub too. So I believe it is more secure to get the current logged UserID from the Session variable I created in MVC controller class (at client's side). Could someone help me with the solution? It will be really helpful if you can show me the complete code. Because I am new to the SignalR thing.
Star
9831 Points
3120 Posts
Re: How to call a SignalR Hub in a Web Api from a MVC Controller class?
Apr 13, 2020 02:06 AM|Brando ZWZ|LINK
Hi chamila91,
As far as I know,if you want to access the server hub in MVC controller, I suggest you could try to use signalr client library.
Then you could use below codes to call the server hub method.
Server code for a method that has no return value
Client hub method:
Best Regards,
Brando
None
0 Points
2 Posts
Re: How to call a SignalR Hub in a Web Api from a MVC Controller class?
Apr 15, 2020 05:55 PM|chamila91|LINK
Hi Brando. Thanks for the reply. Can you also tell me what are these names in your example code above?
UpdateStockPrice
SignalRChatRoom
And please explain below code part:
Star
9831 Points
3120 Posts
Re: How to call a SignalR Hub in a Web Api from a MVC Controller class?
Apr 20, 2020 01:47 AM|Brando ZWZ|LINK
Hi chamila91,
This code is used to log some part of message when connected to the Signlar hub. The stock is my custom model.
You could replace it with below codes:
Best Regards,
Brando