Comments (10)
Yeah, I have a different workaround for you:
public class MyHub : Hub
{
private readonly IHubContext<MyHub> _context;
public MyHub(IHubContext<MyHub> context)
{
// capture an IHubContext<> that isn't tied to the Hub lifetime
_context = context;
}
public Task SendMessage(string message)
{
// Capture the connectionId of the client so we can use Clients.Caller from outside the Hub
var connectionId = Context.ConnectionId;
Task.Run(async () =>
{
await Task.Delay(5000);
// The IHubContext<THub> doesn't have Clients.Caller because it isn't associated with a connection
// So we use Clients.Client(connectionId) to do the exact same thing
await _context.Clients.Client(connectionId).SendAsync("Send", "testing");
});
return Task.CompletedTask;
}
from signalr.
Hi @BrennanConroy ,
Thanks for your assistance.
I have tried the workaround and it's working great.
Hopefully, it will get considered to have it supported on SignalR instead.
from signalr.
This is by design in current release.
We'll consider https://github.com/aspnet/SignalR/issues/3133 in 3.0.
from signalr.
@BrennanConroy could you add the workaround here?
from signalr.
You can achieve long running tasks by using the IHubContext<Hub>
class. See https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-2.2 for some reference on the usage.
from signalr.
Thanks for the confirmation.
As mentioned by @BrennanConroy, the workaround is to send via Middleware, Controllers. Our clients would be expected to use SignalR Hub to stream down their messages.
I have a thought in mind as below:
Assume the long-running task which is a service call(can be in milliseconds/seconds) and can be concurrent messages sent in.
Does the below workaround recommended to be applied since Websocket is Full-duplex?
Server-side code :
public Task SendMessage(string message)
{
this.HandleMessageAsync(message).ConfigureAwait(false);
return Task.CompletedTask;
}
private async Task HandleMessageAsync(string message)
{
await Task.Delay(5000); // to simulate long running tasks(e.g. service/database calls/processing)
await Clients.Caller.SendAsync("ReceiveMessage", message);
}
The above does not block each message and can be sent concurrently.
Please give a shout-out, if this is a terrible idea.
from signalr.
Client-side code :
I'm assuming that is still server side code, and you just meant it is the code that communicates with the client.
That approach would most likely fail because you are possibly using properties of the Hub class after the Hub method has exited and the Hub will be disposed of after each method call.
from signalr.
Yes, you are right. That is still the server side code. Corrected the comment.
The current design seems to be a show-stopper for our use case. Are there any other workaround on this?
from signalr.
Thanks for your quick response & workaround. I'll give it a try and let you know how that went.
from signalr.
Very good information guys... Thanks!
from signalr.
Related Issues (20)
- Restarting TypeScript client does not resubscribe to subscriptions HOT 7
- After update today to VS 15.9 iOS not working HOT 11
- Rename Protocols.Json package to Protocols.NewtonsoftJson HOT 5
- Completion Message in Java client should not require result HOT 1
- [Java] Stream Item payload is an item, not a result HOT 1
- [Java] Crash when close a connections HOT 1
- Connection Fails with accented character in URL HOT 2
- SignalR not supported on Android 8.0.0 and higher HOT 2
- [JAVA] Concurrency and hub connection state HOT 9
- Consider using ReadWriteLock to protect HubConnctionState in Java client HOT 3
- SignalR client not running on android below 26 version HOT 11
- OnDisconnectedAsync(Exception exception) not fired (LongPooling) HOT 2
- Java Client: NullPointerException when calling hubConnection.stop(); HOT 1
- Is it possible to change Response cookies inside OnConnectedAsync? HOT 2
- [Android&Java] on network change HOT 1
- Java Streaming Support HOT 3
- Invalid negotiation response received HOT 5
- Feature: option to disable URL rewriting HOT 2
- I receive messages from my local Hub but not messages via Azure SignalR service HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from signalr.