Consumption of total IObservable from SignalR

Suppose I have an IObservable<Something> server stored in a static field or something else.

Let's also assume that I have a SignalR hub that has a Subscribe method and a signalR client that has a notify function.

 public static IObservable<string> Events; protected void Application_Start() { //dummy observable just to generate events for me.. Events = Observable .Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(50)) .Select(l => l.ToString()); ...snip.. } 

and

 public class MyHub1 : Hub { public void Subscribe() { Clients.All.notify("start"); WebApiApplication.Events .Subscribe(s => Clients.Caller.notify(s)); } } 

and

 myHub.client.notify = function (event) { console.info(event); }; 

What do I need to do to make observable common to all clients? That is, I want each connected client to receive the last 200 response events after they are signed. and then each client must disable the same event in real time.

I believe that the Replay IObservable method should be used somehow. I want it to be like a chat where the user receives the latest x + messages every new event in real time.

Besides actually composing the observable query, what is the best way to store and configure the overall event flow in Asp.NET?

+6
source share
1 answer

This is a very common architectural problem - the combination of a living stream with a "state of the world." What you want to do is use SignalR to broadcast live messages to current subscribers (which is good) and have a separate API call to connect clients to receive historical messages.

In clients, you send logic, which first subscribes to a direct stream of SignalR messages, and then requests the history of past messages ("state of the world") - as a rule, it is best to discard it as a simple ordered list.

There is an indispensable condition for race, which can lead to the receipt of a message in real time and in history - so you should take care to remove duplicates or โ€œcancel the deletionโ€ of your message list.

How you save messages can be treated as a separate issue.

This denouement of the story and the live stream gives flexibility in how you deal with both, and offers opportunities for increased efficiency, such as swapping in the story, rather than capturing it all, for example.

There are several questions and answers that talk about ways to use Rx to combine history and live data - you will need to do this in client javascript, and I'm not very good in rx js.

Take a look at Merging historical and current stock price data with Rx to discuss this issue, as well as these two equivalents of observed transactions? - I have an example code in the latter case, which is a purely .NET script.

+5
source

Source: https://habr.com/ru/post/970089/


All Articles