Skip to content
· 61 min watch
Ian Griffiths By Ian Griffiths Technical Fellow I

Explore Rx.NET, a library for event-driven programming, with Maintainer & MVP Ian Griffiths as he shares the latest updates on this .NET project.

About this talk

On .NET Live

Reactive Extensions for .NET (Rx.NET) is a library for event-driven programming with a composable, declarative model. In this week's episode, Rx .NET Maintainer & Developer Technologies MVP, and endjin Technical Fellow Ian Griffiths, visits to show us the latest developments on this popular .NET project.

Read the transcript

Welcome to On .NET Live

Maira Wenzel: Hello, everyone. It's nice to be back here on another episode of On .NET Live. We will have 60 minutes of unscripted .NET entertainment. Our mission is to empower the .NET community to achieve more. I see we have people from all over the world, but I'm your host Maira Wenzel with my co-host David Pine.

David Pine: Hello.

Maira Wenzel: Cecil Phillip.

Cecil Phillip: Hey, everybody.

Maira Wenzel: I'd like to welcome today's guest Ian Griffiths who's going to be talking about modernizing Rx.NET. But before we jump into that, I want you to just introduce yourself and share a little bit about yourself for us.

Introducing Ian Griffiths

Ian Griffiths: Hello. My name's Ian Griffiths, and thank you very much for having me on. I am a longtime .NET guy so I've been using .NET since before version one shipped. I am the author of the O'Reilly Programming C# series of books. I was one of the first Pluralsight online content authors, and I do a lot of consulting. I used to specialize in WPF work, these days I've moved more into data analytics-type spaces.

These days I work for a company called endjin who are also the sponsors now of the Rx.NET code base, so it's thanks to them, thanks to my bosses that we actually are able to talk about this today. I have a deep love of computing, I've been playing with computers since I was about six, which takes us back a very long way given how grey my beard is, but that's me.

Maira Wenzel: Awesome. I like the extensive curriculum. Thanks to your boss for sponsoring your project. But let's start, maybe, from the beginning and share with our viewers that maybe are not familiar with Rx.NET what it does, what it is, and then we can go from there.

What is Rx.NET and where is it used?

Ian Griffiths: Yes. Rx, in my view, is as fundamental an idea as something like an array. It really is that basic, it does something that is vitally important to computing and has over the years been underserved, to be honest. I would say Rx is useful in any program where things happen. You write programs where things happen. Let's be honest, not all programs are like that, there's a lot of stuff that works around batch processing where data is there and you do stuff to the data and new data comes out of it, nothing's really happening.

There's data there, there's data there afterwards, it's a process, so not everything needs this. But certainly, any user interface-based program has absolutely huge amounts of things happening, and you need stuff to happen in response to that, and that's possibly where Rx is best known. But it's also really useful in things like Internet of Things type applications.

At endjin, we've worked with it in utility companies for example, so we worked with a major broadband network provider using Rx to represent the diagnostic information that the broadband network was able to provide. So you have literally millions of customers with broadband devices in their homes, and every single one of those routers in people's houses is able to generate huge quantities of telemetry about how things are going. And they wanted to do things like anticipate when there were failures coming in the network, but also to be able to do multi-level analysis so that they could tell, when they were starting to get a bunch of faults from certain devices, whether it was just at home, someone accidentally spilled beer into their router or something, or was it actually a fault at the exchange.

Because what you don't want to do is send 30 people out to 30 houses when there's actually a fire at the exchange, that's the wrong response. That ability to manage large quantities of individual information streams and perform analytics that work both at the global level but also are able to remain active at the individual source level as well. That's the thing that Rx is able to model in a way that few other things can. It's, like I say, useful anywhere things happen and you need to do something about that.

It's fundamentally important. It's amazing in a way that computer languages didn't have a way of doing this in the past. Microsoft invented this back in the late 2000s, so around about 2008/2009, this stuff was brewing away. I honestly think it's one of the most significant contributions that Microsoft has made to the world of computing, and you can see how big it is by the extent to which it has been embraced outside of .NET. You look in the JavaScript world in particular, Rx is extremely heavily used, because it is just such a perfect way of dealing with this ubiquitous problem. So I like it, in case you can't tell.

Why hasn't Rx been adopted as widely as LINQ?

Cecil Phillip: That's something that I wanted to tap in on, and you said fundamental, and when I think about fundamental .NET, I think about things like LINQ as an example. At least in 2023 LINQ is a fundamental thing, and when I look at Rx and I look at LINQ and you turn them sideways and squint a little bit, they look like the same thing. They follow similar models and have similar API structures. But I feel like LINQ has been, maybe just because it's in the box, LINQ has been more adopted by a lot of .NET developers. Why don't you think the same thing has happened with Rx in the .NET space?

Ian Griffiths: I would start by saying that LINQ is many things. There's LINQ to Objects, there's LINQ to Entity Framework, there's LINQ to XML, and you can use LINQ in all sorts of things, and Rx was one particular LINQ provider. But I think the way I would cast what I think you're saying is that of all the LINQ providers out there, whether it's data access to a relational database or whether it's just working with objects in memory, Rx has been less strongly adopted, I think. Why is that?

I think part of that, I would say, is probably that it's not in the box, it's not a thing that's right there, you have to go and look for it. I also think that there's a learning curve. It's not necessarily the easiest thing to understand how to use effectively. It's a thing where once people get it, they tend to absolutely love it, but there's a bit of a journey getting to that point. You have something of a eureka moment, but you're like, so this is how I'm supposed to use it, because it feels very different from how we're accustomed to dealing with bunches of things that happen.

Because as developers, a lot of us are accustomed to imperative code that does one thing after another. If you're processing events that happen one thing after another, it feels like you just do that with normal looking code. But actually LINQ in general, and Rx is no exception to this, encourages a much more declarative approach where you describe the way you're processing sets of information. In a relational database that might turn into a query where the database is processing a whole table at a time, and in Rx it turns into sequence processing, but it's expressed declaratively.

I think it takes a while to get used to, in the same way that I think relational database programming takes a while to understand. It's so ubiquitous, it's so common that a lot of people understand it. Although I think actually, maybe these days with document databases becoming more popular, and it's now pushing another... to the mobile, maybe not all developers do understand relational databases in the way that they probably did 15 years ago. But I think it's an interesting example because until you understand how to denormalize your data and how to structure things in a way that works the way the database wants you to work, it's actually quite hard to be effective, but once you get it, it's enormously powerful.

And I think Rx is that thing. I think it is the combination of the slightly steep learning curve and the fact that it has never quite been perceived as mainstream. Those two things have probably meant that it's always been a little bit less strongly adopted. Those are part of the main reasons. But then it has been adopted in the JavaScript world, and I think that might be partly because one of the easiest ways to see why Rx is useful is any user interface application.

Because there you are absolutely dealing with events and there is a very visual outcome of doing so. You can see right away that this connection from this source to that output has had the effect that you wanted, and so it becomes really easy to experiment with it and to understand whether what you've done worked. So you have this very tight feedback loop that helps you to learn it if you're doing user-interface programming. Whereas I think it is harder to learn in other environments, so I think that probably is part of the reason as well.

Cecil Phillip: I guess when I think about where I've seen it used, and I agree with you 100 percent, I've seen a lot in those UI based things, whether it's UI in the browser or on a mobile app or a desktop application. I would even say when I look at it from a JavaScript perspective, a lot of the times, if anyone here is an Angular developer and you're using the HTTP client, there's a lot of Rx things attached to it. But the thing I was always surprised about was we as .NET developers are looking at other people use it, they have figured it out, but for some reason maybe we just haven't cared enough to attach to it.

Because I would assume that they would have less information than we did, because it came from us first. We gave it to you, thank you very much, but now we're just watching other people do it. I agree with you. I think it's an education thing, I think it's a where am I going to use it thing, show me how to do the thing, and then I'll figure it out after that. And I'm hoping part of this conversation would be a little bit of you talking to us about what are things people need to know about Rx on a fundamental level, and then also what are some of the new things y'all are doing to modernize it a little bit.

Updating Intro to Rx

Ian Griffiths: On that, one thing that I wanted to talk about is that we are trying to do a better job of education. Quite a while ago, there was a guy, I think his name is Lee Campbell, let me just check I've got the right name for him. I've lost the page I had his name written down, I think it's Lee Campbell. He wrote a thing called IntroToRx.com and that's still up there, but it was written about 10 years ago, and so it feels a little bit out of date.

At endjin, as well as taking over the ownership of the .NET projects up on GitHub, we also approached him and said, would you mind if we rewrote this? Would you mind if we updated this and brought it up to speed? Because it's a good starting point, but I think people have found it. So when we first announced the latest release of Rx, some people on Reddit said, well, I found this thing here, but it seems to be from 2012, what's going on? It's like, we should probably fix that.

Definitely, education is a big issue, and as a book author, as a Pluralsight author, and a former in-person instructor back when people used to learn things by going into a classroom and listening to someone, I really enjoy explaining stuff. I have a passion for teaching things, so it's one of the things we are hoping to bring to this picture, it's getting better explanations out there.

The origins of Rx in the Cloud Programmability Group

Maira Wenzel: I think we all worked with documentation here in the past, and we know how important it is to have that material. Part of the mission of this show is also to give that platform to folks to get their projects out there and have more visibility. I was wondering, because I don't know a lot about the history of Rx and you mentioned..., so is that something that started with Microsoft and then became a community project later?

Ian Griffiths: Yes, so it originates actually with a team called the Cloud Programmability Group. This was headed up... well, one of the main people behind it was the same guy who invented LINQ actually, whose name I've just blanked on, but he's a very well-known academic in the computing world, and he worked for Microsoft for quite a long time. He was also partly involved in the development of async and await, I think.

David Pine: I think Erik Meijer was his name.

Ian Griffiths: I would say, Erik Meijer. Yes, I can't believe I forgot his name there. Yes, Erik Meijer, he was one of the big brains behind this. And what they were trying to do at the time, the remit of that group, was that Azure was coming. At that point it was very new, and one of the concerns of Microsoft as a whole as a company was, how on earth are people actually going to use this? It's incredibly powerful having this cloud platform that can do all things for you, but how do you actually build software, how are we going to tell people how to use this?

One of the things they wanted to be able to do was to simplify development of software at multiple different layers in the system. What they actually invented looks a lot like what we now call Blazor. They invented that 10 years before Blazor shipped, and they called it Volta, and it was a very similar architecture. One of the things that they actually needed was this ability to pass events from one layer to another. If you had code running in a web browser on a mobile phone, they wanted to be able to have events, like we know and love in .NET, telling you things have happened: button click, this has just been loaded.

They wanted those to be able to flow across machine boundaries. If you wanted the logic to run in some tier in the cloud you could do that, but without having to suddenly go to a totally different programming model. Back 15 years ago that wasn't an option. If you wanted to go across the network you had to deal with sockets, or at least HTTP, which is a totally different thing from a .NET event. They said, well, how could we build an abstraction that is able to represent event-like things, but which is also formally well-defined enough that it would be possible to make it transfer across machine boundaries without losing anything?

That was actually the goal, that was actually the motivating idea behind developing Rx in the first place. Then they developed the abstractions around this, and the IObservable and IObserver interfaces that are the core of what Rx means came out of that, and that was the start of it. And then they realized that actually in a sense it's very similar to IEnumerable. An observable sequence is just one thing after another.

The only difference is that with an IEnumerable, it's the consumer saying, now I'd like the next thing. You foreach over it, it's a so-called pull model. You pull the next item out of the collection when you're ready. With Rx it's still just a sequence of objects, the only thing that's changed is that the source is saying, I've got a thing for you now, I've got another thing for you now. It's pushing data at you, but it's still basically the same model. It's one thing after another, it is a sequence of things, and so they realized they could actually build the entire same set of LINQ abstractions over Rx as were available for IEnumerable, and then from that the Rx project grew out.

Now the Cloud Programmability Group turned into other things, but this project survived, and actually it was one of the first Microsoft projects to be open sourced, I believe. I think it was originally the Outercurve Foundation, as it was back then, and it was up on CodePlex, I think.

David Pine: CodePlex.

Ian Griffiths: Now it's on GitHub of course, as you'd expect, but it was on CodePlex first. I think part of the reason that it has endured is because it was designed for quite different scenarios from how it ultimately got used. They had to come up with an idea that was going to... they had to engineer some abstractions that were actually really well founded and that would pass the test of time, and that I think actually is the key to its success. They really thought about what the right abstraction was, and that is the foundation of everything. Really good foundations are what enable longevity.

What are the benefits of Rx for event handling?

Maira Wenzel: You talked earlier about it being fundamental to almost every type of application you are developing. You're talking about the history, and we've got a question coming from Facebook. Mohammed's asking, what are just the general benefits of using it, differentiating IEnumerable versus IObservable, and one's pull-based, one's push-based. What are the benefits?

Ian Griffiths: What are the benefits of using Rx for event handling? I would say that the answers to that fall into two categories. One of which is that Rx, because it systematizes, there's a standard way of doing things, it's possible for it to provide tools that solve certain problems generally. One problem that comes up with events in user interfaces, for example, is something tells you a thing has happened. So let's say you've got, I don't know, some medical device monitor that sends a message saying, here's the latest oxygen reading on this person's blood, or something, whatever it might be. The software needs to respond to this, but that message could come in on some random thread, you've no idea what thread the library's going to give it to you on. Most user interface frameworks don't like that. If you try and do things to the user interface on the wrong thread, if you're lucky they'll throw an exception, but in the worst case they might just break in subtle ways and just go a bit wrong thereafter.

Rx is able to say, well, we have a way of solving that, by saying you can just take any observable source and say, I would like to observe this source in this particular context. So you can say, I've got a WPF window and I want to observe events from this source in that window's dispatcher. It'll make sure it goes onto the right thread and all the right things happen, and there's a different one for Windows Forms. The Avalonia team have built one that works for that UI, and there's one for UWP. And anyone can write their own, because it's very clear what you do. You just have to provide an Rx operator that abides by all the rules and then does whatever you needed to do to deliver notifications into the right context.

It's that orthogonality, it means you can separate out an aspect of the problem, like getting things onto the right threads. You can separate that out from what am I doing with my data. That then leads me on to the second part of my answer to this, which is that one of the big things that Rx enables is composition of different techniques. And actually, if I may, I'd like to show an example of this to illustrate the thing I'm talking about.

Demo: tracking ships around Norway with AIS.NET

I've got Visual Studio Code here and I've got the Polyglot Notebooks extension installed. I'm just going to run this notebook, and I'm quickly going to tell you what it does first before I start showing you the code. You can see I've got a map here, and if I zoom in on Norway, what's going to happen in a minute is we're going to start seeing lots of little markers appear around the coast of Norway, assuming my demo actually runs correctly. They are going to show me where ships are sailing around the coast of Norway.

I'm going to take a quick peek at the execution status of this to see if it is actually running, how far have we got. It's taking 30 seconds to download a package from NuGet, that is a good start for that. I'm going to close and reload this, because that doesn't normally happen. Let's try again. That's very weird, why is it already running a program?

I think this is going a bit weird, so I'm going to quickly kill that and restart it, if it will let me. That's the problem with running your demos before you start to talk, is that sometimes they then don't work afterwards. This was that folder there. Let's see if this will work this time. I'm going to open up this notebook, and hopefully this time it won't be under the impression that all the cells are already running.

That is looking better, if we can get past this one, there we go. Well, since I've had to go back to the start, I'll tell you what it's doing. I'm going to run everything. This is using a library called AIS.NET, which is also an open source library maintained by my employers, so thank you again to endjin for this. What this does is it connects to a public service generously provided by the Norwegian Government, who provide a thing you can subscribe to that tells you when ships are moving around on that coast. AIS is the Automatic Identification System.

Cecil Phillip: That's awesome.

Ian Griffiths: It's basically GPS receivers on ships... ships are required by law to broadcast their location, their speed, their heading, and various other bits of information to operate in most international waters. So anywhere you see ships, you will be able to pick up AIS messages by just sticking an appropriate receiver up in the air. The Norwegian Government just runs a service where you can connect to a well-known IP address and port number and start receiving messages. Our library provides a client on top of this, which then wraps it in Rx, so you get an Rx observable stream of messages coming from ships moving around from whatever server you've attached to, which is all of Norway in this case. We've then just loaded Bing Maps, and if I scroll down, I'm hoping, there we go, do you see the little arrows?

Cecil Phillip: Yeah, that's awesome.

Ian Griffiths: That's live data. That's all happening in real time. Gradually more and more will appear, because one of the challenging features of this data source is that when they send messages they don't send all the information all the time. There are several different types of messages a ship can send. There's a navigation message which will say, I'm here, I'm pointing this way, I'm moving that way, because when you're on the water those aren't always the same thing. I am going at this speed, and basic information like that.

Reconciling different message types

They don't always tell you the name, because ships tend to move more often than they change their names, and so it doesn't necessarily make sense to include the name in every single message that they send. There's different messages. One of the challenges, if you want to maintain a map like this, is to reconcile those two diverse streams. It's like, this thing here that's just appeared, well, it's told us its location, but it probably told us its name a few minutes ago. We've had to hang on to that.

Logically you've got a stream of "my name is this" type messages, and you'll also get a stream of "I'm here" messages, and we want to reconcile that onto the map to say, I'm here and my name is this, so I can label the things on the map. We've achieved this using Rx, if I scroll down a little bit and start to show you what this looks like. I've had to do a certain amount of jiggery-pokery to enable an interactive map inside a notebook. We'll just ignore all that code, that's just to make things work inside of here live. This is a little complex, but it's quite a good illustration of how you use Rx.

I'm just going to crank the size up a couple of notches, right. That's the receiver host, that's the AIS.NET library wrapper that endjin provides as an open source project, and it has a Messages property, and if I mouse over that you can see it says I am an IObservable. For some reason the notebook doesn't show me the type of message. I can tell you it's IObservable<IAisMessage>. IObservable is the fundamental interface of Rx. It is the thing that any source of events implements.

Measuring the message rate

This is a source of events, so I could subscribe to it. If I did subscribe to it, I would be somewhat overwhelmed. Actually, let me just quickly show you what that would look like. I've got a separate program here written in Visual Studio that uses the exact same source.

David Pine: Could you zoom in a little bit on this one, Ian?

Ian Griffiths: Yeah, sorry.

David Pine: Thank you. No, you're good.

Ian Griffiths: Let me get these things out of the way, so the space is used properly. I've got a few different things in here I can show you. I'm going to show you first of all the rate at which messages are coming in. Uncomment that one and comment these ones out and run this. I hope it will tell me every... no, that's going to tell me every minute, that's far too slow for demo purposes, let me back that one out. Let's do this 10 times a minute.

Also, there was an error in there. Let's just lose that. Ready to go now. Every 0.1 minutes, I've asked this thing to tell me just how many messages it's received from that source. That's every six seconds, 113, so multiply that by 10. This is somewhere between 1,000 and 1,500 messages a second coming out of this source, so relatively busy. There's quite a lot going on here.

There's quite a lot to keep track of. If you just subscribe to this and just print out messages, you'd be drowning in data and it would be completely incomprehensible. What we tend to do with Rx is that we use operators to manage the data. We can use various LINQ operators to process it with a bit more sanity. I'll get back to the map, but I want to show this one first because this shows things broken down in slightly more detail.

Partitioning the stream per ship with GroupBy

If I uncomment this one, here I'm using GroupBy. This is a standard LINQ operator, you get this in LINQ to Objects, you get this in LINQ to Entity Framework, and so on. This basically says, I've got a bunch of things here, happens to be an observable sequence of AisMessages in this case, and I'd like to partition them into groups, and I'd like to group them by the vessel identifier. The MMSI is something like, I can never remember, marine mobile something information. The S probably doesn't stand for something, but it may as well do.

It's a unique identifier. Each ship has its own different ID, so what this does is it partitions the fire hose of messages coming from all the ships in Norway, it partitions them into groups with one group per ship, so I get observables of that form. Every time it sees a ship with an ID it hasn't seen before, my top-level observable is going to emit a new group for that ship and that ship alone. But that group is then going to emit an AisMessage every single time that particular ship broadcasts a new message.

Detecting navigation status changes with DistinctUntilChanged

Then armed with this I can do things like ask, well, when does the ship change its status? I'm going to run this now, because this actually produces messages quite slowly, often. I'm going to move that off the screen while I talk about this, and hopefully it will produce a response by the time I'm done. What are we doing here? Well, here I'm saying, well, I'm aware that there are several different types of messages.

I'm going to focus in on a certain type of message that I happen to know reports the navigation status of a ship. That tells you things like, is it moored? Is it moving under its own engine power? Is it sailing somewhere? Is it being towed by a tug? Is it fishing? It's got lots of different well-defined statuses the ship can be in.

The navigation messages, so the ones that tell you where you are, which way you're pointing, how fast you're going, also happen to report this navigation status as well. But it doesn't change very often. Ships don't tend to flip back and forth between being moored and anchored and sailing and fishing a lot. If you think about what a sailing boat does, it will be moored in the morning, and then it will start sailing out to sea, and then it will be fishing for several hours, then it will come back and then it will moor again, so every few hours you expect a change in status.

What if you wanted to report just the status changes, how do you do that given the fire hose? Because this is every message from every ship that's visible to the receivers. Saying when one particular ship has changed status is a little tricky. But we can do it because we've initially partitioned the stream of messages into a per-vessel group. This perShipObservables says, I am going to emit one nested observable stream for each individual ship that I see.

Then within that we're going to say, here is a stream of messages for one ship alone, I would like to just look at this message, so give me the messages OfType this. Then I only want to see changes, so the DistinctUntilChanged operator in Rx says, if this thing emits a value but the value looks the same as the last one, then I don't care, I don't want to know, I only want to see when it's changed. Basically, this says, tell me every time this stream changes, and you can pass in a callback to say what it is you're actually interested in. So the speed might have changed, but I don't care, the location might have changed, I don't care, I only care about the navigation status changing.

This is going to filter down that stream, with hundreds or thousands of messages the ship sends out, down to just the ones where its navigation status changed. Remember this is all happening within the individual ship message stream. We have the fire hose, we partition that into individual ship streams, then for each of those individual streams I've said, I'd like to watch each of those and see when something changes.

That's essentially the logic. Now doing a little bit more on here, I say, every time it changes state, I'd like to see its current status and its previous status, so can you collect this in a sliding window of size 2 for each thing? So every time it changes I'm going to see the current value and the previous value next to each other. When I come to actually print out a message I can say, what was the previous navigation status, so looking at the first of the two messages, what was its navigation status, and then look at the second, what was its one.

Fanning out and fanning back in

Then the SelectMany actually flattens it all back out again. Having done that per ship processing and filtering, and saying I'd like to chuck away all the messages except where something interesting happens, can you then flatten that back down into one global stream of all the messages from all the ships? This is much lower volume, in fact such low volume that none of the ships in Norway have changed status in the time I've been talking. I generally leave this running for about 20 minutes, unless you run it at the times of day when everyone's leaving or entering the harbor, so it does eventually start doing things.

But for me the interesting thing here is that we've taken a message source that produces thousands of messages a minute and reduced it down to one that tells me just the things I'm interested in. This is a good example of the ability to combine global and local inspection, because this DistinctUntilChanged, this is just looking at an individual ship. Because if I apply this across the whole fire hose, it will tell me nothing useful. It would say, the status of this message is different from the status of the last message, but that's a completely different ship, so that tells me nothing at all.

I want to know when did this particular ship change its status, that's what I care about, and this is a technique in Rx known as fan out and fan back in again. I've got a linear stream of all the messages. I fan that out into a series of groups, so it's like I've got not one stream but loads of streams, then I apply the processing on each of the streams within that fan, and then after I've done my processing I merge it back into one thing that contains this low volume but high-value event stream. And annoyingly nothing still has changed, this is not a good time of day for this demo, but eventually it will.

Maira Wenzel: We will take your word for it, we believe you.

Combining message streams with CombineLatest

Ian Griffiths: Which is why I have the other demo, because this one at least is actually showing things. So this one, I think the map's going to be quite busy now. It's definitely a real data source, there's definitely things happening here, but they're all doing what they're doing either in Norway or out at sea, apparently. That's all going on. There's similar processing happening in this map one.

If we look at this one, you'll see again I'm using group by MMSI. It's basically taking the single linear stream of all the messages from all the ships and fanning those out into a series of individual observables, one observable for each ship, so it's a grouped observable because it's produced by GroupBy, and then I can specify a stream of processing within that. This is slightly more complicated, this is the one that's rendering the map. It's having to combine several different types of messages.

It says, I want the navigation data that tells me where it is, where it's pointing, where it's heading, how fast it's going. I also want the vessel name, and actually I want the ship type as well. The color of the arrows is telling me something about whether it's a tanker or a ferry or a fishing vessel or something else. There's a whole bunch of different ship types, so we're using that to color the map in.

These three kinds of information here, all three of those come from different message types, it turns out. We're basically saying, from my per vessel stream that the GroupBy has given me, I filter out all three of these kinds of messages into three independent streams. So here are all the location and speed changes, here are all the names for this particular vessel, and here is its report of ship type. Then I combine that together with this Rx thing that says, I've got several streams here, I'd like to merge them back into one where I get the latest value from each of the input streams.

CombineLatest says, whenever any of these three streams produces a value, give me the latest value from all three of them at any one time, and then this ultimately is what feeds into populating the map. It's this ability to use the various filtering and partitioning and processing operators on your events that distinguishes Rx from regular .NET events. You really couldn't express this declaratively with ordinary .NET events. And this compositional approach, where you take lots of different small utilities like GroupBy, filtering, aggregation, windowing, and so on, and you combine them in interesting ways to achieve the effect that you want.

That compositional style is really the essence of Rx, and it's the thing that takes the longest to get used to. Understanding how to combine the ingredients you have to produce the result you want actually takes some learning, but once you get some familiarity with it, it's really powerful.

Questions from the chat: RxJS, MVVM and ReactiveUI

David Pine: I think you sparked a lot of interest here, because our chat just started blowing up. There's many questions, and people are looking for more resources, if you have additional links that you can provide for further education after the fact. Earlier on I did want to call attention to the fact that someone had mentioned this seems a lot like RxJS, and there's a bit of irony there, because this is the predecessor.

Ian Griffiths: It seems like RxJS because RxJS actually was a carbon copy of this, that's why they seem similar.

Maira Wenzel: There's a question about whether or not this is compatible with the CommunityToolkit.Mvvm. I would assume so, since it's .NET.

Ian Griffiths: Yeah. I mean, this is additive. It adds capabilities. If you're using .NET, you can use this. I think the more subtle question is, is there any integration between this and whichever UI framework you are using? The answer to that is probably less so. It's not that you can't use MVVM. It's more that with MVVM, it depends on which particular flavor you're using, but you might not necessarily have intrinsic integration. But what I would recommend is looking at ReactiveUX... let's just bring up a web browser here.

Cecil Phillip: ReactiveUI maybe.

Ian Griffiths: Sorry, ReactiveUI.

Maira Wenzel: Someone asked about that specifically too. They said, what's the relationship between these two?

Ian Griffiths: ReactiveUI, this is not a thing I am directly involved in, but we consider them to be a really important customer of Rx. One of the things that we've been doing since we've taken over is trying to make sure that we are doing our best to address any issues that they have with the Rx codebase. We've done a few things that are specifically for them, and anticipate doing more of that.

What is ReactiveUI? ReactiveUI is a .NET framework for doing user interface logic specifically with Rx. It is designed with Rx at its heart. If you want to do MVVM with Rx, then ReactiveUI may be exactly the framework you are looking for, and I would strongly recommend going here and having a look at it, because this does give you the power of those two concepts combined together. It's an amazing piece of work. There's a whole lot in here. It's really, really good, so yes.

What is IQbservable?

David Pine: Awesome. Then Tom is asking about the IQbservable.

Ian Griffiths: IQbservable is how it's pronounced.

David Pine: Perfect.

Ian Griffiths: How best to talk about this?

Maira Wenzel: That was a typo, the Q there?

Ian Griffiths: Yes. People normally do. What is IQbservable?

Cecil Phillip: I just assumed that it was probably like the IQueryable, where you basically transmit the intent over the wire.

Ian Griffiths: That's pretty much it, yeah, but let me show you specifically what it is. I should probably quickly just actually introduce the core interfaces that are at the heart of Rx.

The core interfaces: IObservable and IObserver

I've just added the System.Reactive library to my project so I can tell you. For example, IObservable<long> ticks = Observable...

David Pine: Well, to be clear, the IObservable and IObserver interfaces aren't part of Rx.NET. Aren't they System?

Ian Griffiths: They are the heart of Rx. They're not part of System.Reactive though. They are the heart of Rx, but they are built into the .NET runtime libraries, so they are still the heart of Rx.

David Pine: Yes.

Ian Griffiths: It's just they happened to be able to build them into the framework. Let me just quickly get an actual example so I can show you something specific. This says I would like an IObservable of type long, which I'm going to ask Rx, the Rx library, System.Reactive, to build me one of those just by giving me a new tick every second. And if I just subscribe to this, WriteLine, this is absolute Hello World Rx.

I've got an observable source and I've subscribed to it with an event handler that says what I want to do, and I have to somehow make sure my process doesn't exit because it's all asynchronous. I'm just going to do a Console.ReadLine to keep things alive, and if I run this, this will start doing a tick every second. That's about as basic as it gets with Rx. I've said I've got an observable source raising an event every second.

What does this actually look like? IObservable is a pretty simple interface that has a single method called Subscribe, which I called down here, except I actually called an extension method that helped me a bit. You subscribe to an IObservable by providing an IObserver. How do you observe? You have an observer. What this looks like is an interface with three methods: OnNext, OnError, and OnCompleted.

They're listed in the least helpful order they could possibly be listed for you. OnNext is called every single time the source has a value for you. Each time you see that tick, that's because the observable source called OnNext on my observer. If something goes wrong, it's going to call OnError, and if it has run out of things to give me and knows that there's nothing more, it calls OnCompleted to let me know that it's done. Not all sources do that. Observable.Interval would go on for as long as you care to remain subscribed to it.

That's pretty much it. That's the whole of the basic programming model. Observables are things you can subscribe to, and you have to supply an observer, or some utility that implements IObserver for you, which is what I've done here. This just wraps my delegate in an IObserver interface and calls it. That's IObservable. This is the basics.

IQbservable and expression trees

What about IQbservable? Not a typo. Qbservable. You can basically write the same thing. What's the difference? Q, and the answer is that it compiles. IQbservable looks pretty... I mean, it implements IObservable, so it's very similar, and it doesn't add a great deal more. We've got IQbservable inherits from IObservable, let's see. You can do anything with an IQbservable that you can do with an observable, and then it also inherits from this non-generic base interface, which will look familiar to anyone who's done any IQueryable work in the world of LINQ to data sources.

The basic idea with this is that when you have an IQbservable, if I can find... actually sorry, this is not a great example. But the basic idea with this... sorry, I've realized I should have done something more complicated, and we're going to run out of time if I do it. But the basic idea is that rather than actually building runnable code, it causes the C# compiler in general to emit query expressions.

You may be familiar with doing this thing. If you say I'd like a function called... let's call it d for double, x goes to x*2, that just generates some code. That just gives me a function that has the IL for that code baked into it. But if I wrap it in this, I get something completely different. I get a thing that describes the shape of this expression.

If I let that run to there, what's happening? Yeah, it didn't like my code, because I forgot to do the thing that I didn't have time for. If we just let this run and hit that line of code, what is d here? Well, d is actually a description of my expression. If I just zoom in a little bit on this so people have got a chance of seeing it at home. It doesn't work very well in multi-monitor systems, this zooming. We can see it knows what the expression is. It says it's a lambda that goes to x*2, and we can actually start to drill into this.

If you drill down inside it, it will be a complete description. It says, hey, the body of this expression is actually a multiplication operation, and the multiplication's left-hand operand is a parameter called x, and its right-hand operand is a constant whose value is two. Essentially, rather than turning that into code, it's just built an object model that is a tree of objects describing what the expression looks like.

Reaqtor: Rx in the cloud

Why is that interesting? That is interesting because it enables you to move the expression from one machine to another. There is a related project that we also maintain called Reaqtor, which is spelled Reaqtor with a Q. If you want to find out about this, you should go to reaqtive.net, which is this site here, R-E-A-Q-T-I-V-E.NET, and this is essentially Rx in the cloud. This is a way of hosting long-running subscriptions.

If you want to have a thing that, for example, observes a temperature sensor in some piece of industrial plant, and that raises notifications when something goes out of range, you want that to keep running forever, for years at a time, and you don't want it to shut down just because you have to apply some updates to a device. Reaqtor built on top of Rx to say, well, we're going to need persistence. We need things to be able to be remembered. We need durability, so if one machine goes down we can bring it back up again and have all the same state there as we had before. That's what Reaqtor does.

But to be able to do this, it needs to know exactly what you're doing with Rx. Rather than you just having compiled IL, compiled code that represents what you did, it needs to see the description of what your query looked like, so that it can reconstitute it if it has to reboot a server. Look, hang on a second, I have to show you this. If I can move it onto the screen, it was real.

Maira Wenzel: You got a message.

David Pine: I love that. You decided to leave it running to prove it.

Ian Griffiths: Yeah, I was looking at it on another screen a lot, so finally an interesting thing happened.

How does Rx relate to Orleans and Akka.NET?

Cecil Phillip: So what's really interesting about Reaqtor and the expression trees, and transferring the intent of your Rx... Jose was actually asking, how does Rx relate or work with Microsoft Orleans or Akka.NET? It sounds like Reaqtor is doing that to an extent.

Ian Griffiths: Well, how does it interact with those other frameworks? They're quite different programming models. So with Reaqtor you don't put any of your own code into the compute farm that hosts Reaqtor. You just describe the queries you'd like it to run, the subscriptions that you want, and then you tell it what the ingress and egress points are. You might say, I've got an Azure Event Hub here, I'd like to connect that in as the observable source for this query. Here's my query, and then please feed the results out through this Azure Event Grid, for example, or whatever it is you might want to do.

The stuff that runs inside of Reaqtor is purely the Rx expressions. Like these queries I've been showing you, the group-bys, the filtering, all that stuff, that's what runs, and certainly you don't get to actually upload any of your code into those things. Whereas with things like Orleans and Akka, it's much more of, I deploy my code into this environment, and then it provides me with a programming model that makes certain guarantees.

They're only similar at quite a high level of abstraction, in the sense that they are well-defined models for managing long running computations on a server farm, but the actual mode of consumption is very, very different, I would say. I know there were some discussions between the Orleans group and the original creators of Rx. I don't know if anything has come from that, they may or may not have plans, for all I know, but it'll be interesting to see what might happen, because you could easily imagine wanting to combine these two.

The idea of having a Reaqtor-style stream that then feeds into one of these other programming models, and then use those as a source downstream, that would be great. Today I'm unaware of anyone having done that with this. It's certainly technically possible, but no one I know of has done the work, or if they have, they've not been public with it.

Durable subscriptions

Maira Wenzel: Very timely, Paul's asking about Reaqtor. He's like, is it like a durable task framework?

Ian Griffiths: It is a durable subscription framework. I think it actually persists... let me go back to my previous example. Let's stop debugging, and let's load the last example I did that actually worked, the ship status change one. The point at which you do a subscribe, so this thing down here, this is the point where we're saying, okay, I've defined what my query is, but now I actually want to subscribe to that and do something in response to it. That's actually the thing that gets captured by Reaqtor.

Before you've actually subscribed to a thing, it doesn't really exist. It's like a tree falling in the forest. Until someone subscribes to listen to it, it doesn't actually happen, but it's actually a bit more real: the tree doesn't even fall over until you start listening to it. It takes that and slightly overstretches the reality of it, but that's how it is.

When you define an observable source like I've done here, nothing is actually happening yet. I've just said, here is the definition of this observable that tells me about the ship status changes. This is all just an expression that says what to do, but it's only when I subscribe that anything actually begins to happen, because up until that point there's nowhere for the data to go, so why would it do anything?

Then Reaqtor with a Q takes that a level further and says, we will create a persistent thing at the point where you subscribe. It enhances this with things like identifiers, so a subscription actually has a URI associated with it, so you can go find it again later if you need to.

Modernizing Rx.NET: what endjin has done

Maira Wenzel: I'm just now realizing that we're approaching the top of the hour, and we haven't even talked really about modernizing very much. That was the emphasis of this episode, and we're going to definitely have to have you back on, because this is great. There's a lot of engagement, a lot of great questions.

Ian Griffiths: I'd like to talk very quickly about what we have done then, if I may.

Maira Wenzel: Absolutely.

David Pine: Please.

Ian Griffiths: Towards the end of last year, we approached the previous maintainers of Rx because it had become more or less abandoned. Up until .NET 5, they'd managed to do a very good job of keeping releases going again and again, but for various reasons the people involved just didn't have the time to keep on that treadmill. We went in the space of only a year-and-a-half from Rx.NET being apparently completely up to date to people thinking it was dead, because it hadn't had a release. It was particularly unfortunate that Rx 5 had the same version number as .NET 5, so everyone thought that when there was no Rx 6 that it had died.

That wasn't actually true, but it was unhelpful. endjin stepped in, and they agreed to let us work on this thing, and so we became maintainers of the project, and we set out a roadmap for what we wanted to achieve. Back at the beginning of the year, we said, here's our long-term vision. So here's all the various different flavors of .NET that we need to think about. There's .NET Framework, there's .NET, there's Xamarin, there's UWP, which has been supported in Rx forever and we're not about to remove support for that, there's Unity.

We have to think, what do all these different platform lifecycles look like, and then what do we have to do to accommodate that? We then said, here are a bunch of problems that we know about as well. So there had been no releases for a while. The software didn't actually build if you opened it on current versions of the SDK and the tooling.

There are certain problems that UI frameworks have been having with more recent versions of .NET, so it doesn't play well with trimming, it doesn't play well with ahead of time compilation. There was an ever-growing backlog of issues that was getting... Also, there's a whole async variant of Rx that's been in an experimental state forever and hadn't really been...

Progress on the roadmap

We said, we're going to address these things over the next year, and so we said, we're going to get a new release out. We are going to bring it up to date with current tooling. We're going to fix the... problems. We're going to deal with the stalling of the backlog, and we're going to do async Rx. We've achieved most of these.

We did that. Rx 6 came out earlier this year. It builds properly on the current versions of everything. This is the one we haven't dealt with yet. This is a hard one, avoiding code bloat problems on things like... We are making progress on this.

There is still a big backlog of issues, but we do now respond to them. There is a preview version of async Rx now available on NuGet, so you can experiment with that if you want to, but it is a preview, not beta, it's way before that. It's not ready for consumption yet, but you can go and look at it. We didn't go into more detail on that, but that's essentially what we've done.

We've managed to achieve four of these five goals, and it's not the end of the year yet, so we're hopeful we will actually manage to do all these things. Hence it opens questions about what to do in the longer term than that, but we wanted to get these things done because they were the ones most pressing for the community, so yeah, that's what's going on.

Cecil Phillip: Yeah, and it seems like the next pressing issue would be to have you guys just do your own conference. Do it, Rx stuff.

Ian Griffiths: I'm not sure my boss would be up for sponsoring that. I can ask him, but...

Maira Wenzel: You can come any time. You can come back and do more sessions, and then we have a mini conference.

David Pine: There we go.

Ian Griffiths: Yeah, I can talk for days about this, as you probably already noticed.

Updating Intro to Rx and wrapping up

Cecil Phillip: Awesome. One last thing I wanted to ask you really quick. In the beginning, you mentioned one of the things that your team was working on was improved education. Can you talk about that really quickly? Because I'm sure folks, when they leave, would love to know, well, I want to learn the thing, where can I go and learn about some of the stuff that you guys are doing?

Ian Griffiths: This is unfortunately a work in progress, so we have this thing. There is the site Intro to Rx, which you did link to earlier, but we haven't updated it yet. I opened it at a random chapter because I was working on it today. We are basically going through this. Actually, the content is not bad, it's just a little bit out of date, so it goes through all the parts of Rx.

It talks about the core types, IObserver and IObservable. It talks about how to create observable sources. It talks about all the different ways you have of processing data, and then goes on to more pragmatic concerns, like how do you deal with threading? How do you get back out into the outside non-Rx world? How do you handle errors? How do you do testing? Various things like that.

There's a lot of information in here. We are probably still a couple of months off from actually having the updates out there, I'm afraid, so it's not yet live. If you go there today you will find the very old content, but we are working on it and we want to get that out there as soon as we can. But I also have to do paying work from time to time, so it's going to be a little while. I'm also working on an update to my Programming C# book, so we're getting through this as fast as we can.

Cecil Phillip: You're a busy man.

Ian Griffiths: Yeah.

Maira Wenzel: Awesome. I think from the chat you can see that there was a lot of excitement. I think you got the wheels in each person's brain spinning, because people are thinking about how can they integrate it into their projects, thinking about how can I use this, so it's been awesome. So please come back at any point that you want to share more. I think we can do more demos, or whatever... people would like to see.

I want to thank you for coming here today, but also thank you everyone for watching and being such a great community, and asking questions, that's what makes the show shine. You can go and check other great .NET live streams and videos at dot.net/live. Tune in next week, we're going to have one of our hosts, Louis..., and he's going to be talking about OpenAI for .NET developers. Thanks for hanging out with us. If you want, you can also submit your own talk like Ian did, and it would be awesome to have you on the show as well. Thank you, everyone, take care.

David Pine: Thank you, bye.

Ian Griffiths: Thanks very much, bye.

About the presenter

Ian Griffiths

Technical Fellow I

Ian Griffiths

Ian has worked across an extraordinary breadth of computing - from embedded real-time systems and broadcast television to medical imaging and cloud-scale architectures. As Technical Fellow at endjin, he brings this deep cross-domain experience to bear on the hardest technical problems.

A 17-time Microsoft MVP in Developer Technologies, Ian is the author of O'Reilly's Programming C# 12.0 and one of the foremost authorities on the C# language and high-performance .NET development. He's a maintainer of Reactive Extensions for .NET, Reaqtor, and endjin's 50+ open source projects.

Ian has created Pluralsight courses on WPF fundamentals, WPF advanced topics, WPF v4, and the TPL, and has given over 20 talks at conferences worldwide. Technology brings him joy.