SignalR 2.1.0: The connection has not been established - c#

I have a ASP.NET Web Application with a simple HTML page and some JavaScript to communicate via SignalR. That works fine.
Now I'm trying to call a method on the Hub from another project (in the same solution) and by using the .NET Signalr Client Api:
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
connection.Start();
hub.Invoke("SendMessage", "", "");
The last line causes InvalidOperationException: The connection has not been established. But I am able to connect to the hub from my JavaScript code.
How can I connect to the Hub by using C# code?
UPDATE
The moment after writing this post, I tried to add .Wait() and it worked!
So this will do:
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
connection.Start().Wait();
hub.Invoke("SendMessage", "", "");

HubConnection.Start returns a Task that needs to complete before you can invoke a method.
The two ways to do this are to use await if you are in an async method, or to use Task.Wait() if you are in a non-async method:
public async Task StartConnection()
{
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
await connection.Start();
await hub.Invoke("SendMessage", "", "");
// ...
}
// or
public void StartConnection()
{
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
connection.Start().Wait();
hub.Invoke("SendMessage", "", "").Wait();
// ...
}
The "How to establish a connection" section of the ASP.NET SignalR Hubs API Guide for the .NET client. goes into even more detail.

Related

azure functions server (isolated-process): Invoking SignalR group functions from client

I am trying to add a SignalR client to specific SignalR group on an azure functions server (isolated-process). The server side code that I am trying to invoke is the following:
[Function("SendToGroup")]
[SignalROutput(HubName = "serverless", ConnectionStringSetting = "AzureSignalRConnectionString")]
public static SignalRMessageAction SendToGroup([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
using var bodyReader = new StreamReader(req.Body);
return new SignalRMessageAction("newMessage")
{
Arguments = new[] { bodyReader.ReadToEnd() },
GroupName = "groupToSend"
};
}
I have tried invoking the above from the client side in C# with the following code to no avail:
HubConnection _connection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();
Dispatcher.Dispatch(async () => await _connection.StartAsync());
// This does not work
await HubConnectionExtensions.InvokeAsync(_connection, "SendToGroup");
In my searches, I have not found a single C# client code sample that shows how to connect to and remove oneself from signalR hub groups. I would really appreciate being pointed in the right direction.

Azure SignalR Blazor app not receiving messages

I'm looking at incorporating Azure SignalR functionality into my .net core Blazor web application. To this end i've been following this tutorial - Azure Signalr Serverless. This is working fine - i have a project running the Azure functions app and can start up two browsers and have a chat session. What i'm trying to do is add the ability to receive these message notifications from the Azure signalR hub that's been configured into my Blazor app. I've added the following code in Index.razor.cs that mimics the javascript code in the example client:
public class IndexComponent : ComponentBase
{
private HubConnection _connection;
public string Message;
protected override Task OnInitializedAsync()
{
_connection = new HubConnectionBuilder()
.WithUrl("http://localhost:7071/api")
.Build();
_connection.On<string, string>("ReceiveMessage", (user, message) =>
{
Message = $"Got message {message} from user {user}";
this.StateHasChanged();
});
_connection.StartAsync();
return base.OnInitializedAsync();
}
}
The example javascript code btw is:
const connection = new signalR.HubConnectionBuilder()
.withUrl(`${apiBaseUrl}/api`)
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on('newMessage', newMessage);
connection.onclose(() => console.log('disconnected'));
console.log('connecting...');
connection.start()
.then(() => data.ready = true)
.catch(console.error);
So the problem is that my Blazor app never receives any message notifications sent from the javascript chat clients (so the _connection.On handler is never hit). What am i missing in my Blazor code ?
Ok so this is what i needed to do to get it to work in my Blazor app:
_connection.On<object>("newMessage", update =>
{
Console.WriteLine(update);
//Message = update;
});
I needed to subscribe to the 'newMessage' target (since that's the JS is sending on) and also the type that's being posted isn't a string but a JObject type which i would need to deserialize to the correct type.

Calling SignalR from API at another project - No error nor notification

I have a WebSite integrated with SignalR. It functions well, and it has a button which sends popup notification to all clients who are online. It works well when I click on the button.
My API is in another project but in the same Solution. I want to send the above notification by calling from the API side. Basically, a mobile app will send a request to API and then API will send a notification to all online web clients.
Below code runs and not gives the notification nor any error.
Is this fundamentally correct? Appreciate your help
API code (at WebAPI project)
[HttpGet]
public IEnumerable<string> WatchMe(int record_id)
{
GMapChatHub sendmsg = new GMapChatHub();
sendmsg.sendHelpMessage(record_id.ToString());
return "Done";
}
C# code (at Web project)
namespace GMapChat
{
public class GMapChatHub : Hub
{
public void sendHelpMessage(string token)
{
var context = GlobalHost.ConnectionManager.GetHubContext<GMapChatHub>();
context.Clients.All.helpMessageReceived(token, "Test help message");
}
}
}
Home.aspx file (at Web project)
var chat = $.connection.gMapChatHub;
$(document).ready(function () {
chat.client.helpMessageReceived = function (token,msg) {
console.log("helpMessageReceived: " + msg);
$('#helpMessageBody').html(msg)
$('#helpModal').modal('toggle');
};
}
You can not call that hub directly. Firs you need to install the .net client for SignalR from nuget. Then you need to initialize it like this :
[HttpGet]
public IEnumerable<string> WatchMe(int record_id)
{
using (var hubConnection = new HubConnection("your local host address"))
{
IHubProxy proxy= hubConnection.CreateHubProxy("GMapChatHub");
await hubConnection.Start();
proxy.Invoke("sendHelpMessage",record_id.ToString()); // invoke server method
}
// return sth. IEnumerable<string>
}
And opening a new connection per request may not be good idea you may make it per session (if you use) or static or time fashioned.

signalR with multiple client connections

I have written an Application where I am using SignalR. I am sending connectionId from Client to Server(controller).
Everything is working fine with single browser (request will sent to server with connectionId="conn_1") and signalR is sending response to only conn_1, but when i open new browser and send a request from that client the previous connection gets disposed. Which means only one connection with particular connectionId remains alive.
Is there any way SignalR can not dispose and send response to both with data they want?
I am new to SignalR and would really appropriate any help or guidance.
Angular SignalRService to start connection with server
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(this.paymentDraftHubUrl)
.build();
return this.hubConnection
.start()
.then(() => this.hubConnectionStatus = 'Connection started')
.catch(err => (this.hubConnectionStatus = 'Error while starting connection: ' + err));
}
sending connectionId from client component to Api
this.signalRService.startConnection().then((connection) => {
this.connectionId = connection.toString();
//Calling Api
this.getAllTransactionException(
this.connectionId,
this.pageNumber,
this.pageSize
}
MyHub class in C#
public class PaymentDraftServiceHub : Hub, IPaymentDraftHub
{}
Controller for API
using timer to keep calling repository for new data,
[HttpGet]
[Route("GetCsrTranactions")]
public IActionResult GetCsrTranactions([FromQuery] TransactionExceptionDataRequest queryParams)
{
TimeManager.Dispose();
var timerManager = new TimeManager(async () =>
await _paymentDraftHub.Clients.Clients.Client(queryParams.ConnectionId).SendAsync(SignalRConstants.TransferPaymentDraftServiceData, await _paymentTransactionRepository.GetCsrTranactionsAsync(queryParams)));
var response = new ResponseMessage { Message = "Accepted", Code = "201" };
return Ok(response);
}
Client can have multiple connections with multiple connection IDs if client connect from multiple browser windows or tabs.
According to the code you provided, we can find that you just pass connection ID of SignalR client within current active browser tab/window to your controller, and in your controller action, you use this code snippet .Client(queryParams.ConnectionId).SendAsync() to send message to a specific client, so other browser windows or tabs would not receive the message.
If you'd like to send message(s) to a client with multiple connections, you need to map SignalR users to connection Ids and retain information about users-to-connectionIds mapping, then you can get all connectionIds of a client and send messages to that client with with multiple connectionIds, like below.
//code logic here
//to get all connectinIds of a client/user
//from user-to-connectionIds mapping table
await _paymentDraftHub.Clients.Clients(connectionIds_here).SendAsync("method_here",args_here);

How to send Parameter/Query in HubConnection SignalR Core

I'm trying to add parameter into connection to signalr.
I'm using Builder to create my Client connection and start it:
var connection = new HubConnectionBuilder()
.WithUrl("http://10.0.2.162:5002/connection")
.WithConsoleLogger()
.WithMessagePackProtocol()
.WithTransport(TransportType.WebSockets)
.Build();
await connection.StartAsync();
I want to send a simple parameter in this connection:
Something Like:
"Token": "123"
In my server side i think i can take this parameter from HttpContext:
public override Task OnConnectedAsync()
{
var httpContext = Context.Connection.GetHttpContext();
var token = httpContext.Request.Query["Token"];
return base.OnConnectedAsync();
}
Any idea of how to send this parameter?
Thanks.
I have found how to do this after much research:
On my build i just send the token from url connection.
Like this:
var connection = new HubConnectionBuilder()
.WithUrl($"http://10.0.2.162:5002/connection?token={token}")
.WithConsoleLogger()
.WithMessagePackProtocol()
.WithTransport(TransportType.WebSockets)
.Build();

Categories

Resources