Ayende @ Rahien

It's a girl

Awesome feature of the day, RavenDB Changes API

This was a really hard feature. I’ll discuss exactly why and how in my next post, but for now, I pretty much want to gloat about this. We now have the ability to subscribe to events from the server on the client.

This opens up some really nice stories for complex apps, but for now, I want to show you what it does:

 store.Changes()
      .DocumentSubscription("orders/1293")
      .Subscribe(Reload);

You can add subscribe to multiple documents (or even all documents) and you can also subscribe to changes in indexes as well.

Why is this an awesome feature? It opens up a lot of interesting stories.

For example, let us assume that the user is currently editing an order. You can use this feature to detect, at almost no cost, when someone have changed that order, saving the user frustration when / if he tries to save his changes and get a concurrency exception.

You can also use this to subscribe to a particular index and update in memory caches on update, so the data is kept in memory, and you don’t have to worry about your cache being stale, because you’ll be notified when it does and can act on this.

You can even use this to watch for documents of a particular type coming in and do something about that. For example, you might setup a subscription for all alerts, and whenever any part of the system writes a new alert, you will show that to the user.

The last one, by the way, is a planned feature for RavenDB Studio itself. As well as a few others that I’ll keep hidden for now Smile.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Jeff Doolittle
07/19/2012 10:44 PM by
Jeff Doolittle

nice work. extremely compelling and extensively useful for a host of scenarios.

Matthew Bonig
07/19/2012 10:48 PM by
Matthew Bonig

http://suzumiyayuki.files.wordpress.com/2012/01/mind-blown.gif

Thomas Levesque
07/19/2012 10:49 PM by
Thomas Levesque

Very nice!

However the API seems redundant; I would change it to this:

store.Changes() .Document("orders/1293") .Subscribe(Reload);

Paul Stovell
07/19/2012 11:02 PM by
Paul Stovell

This is awesome. I have a task queue implemented on top of RavenDB, and I have a timer polling the database every few seconds to check for new tasks. Being able to subscribe to new tasks would be amazing.

Will it work on embedded document stores?

Paul

Matthew Bonig
07/19/2012 11:06 PM by
Matthew Bonig

@Thomas: I think the syntax as is works well since it's not necessarily a document that you may subscribe to but in fact something wider. This leaves that distinction open.

Wilson Mead III
07/19/2012 11:11 PM by
Wilson Mead III

This is exactly what I was looking into a day or so ago. And since I saw signalR embedded in there, I was hoping that would be why.

Now to figure out how to get it to work on a web server :)

Glenn
07/19/2012 11:22 PM by
Glenn

Kick ass feature!!

Ken Egozi
07/20/2012 12:11 AM by
Ken Egozi

SignalR ftw

this can do wonders to cache invalidation.

btw these days if you had to re-implement ravenDB from the floor up, you might have had Storage+Subscription => feeding indexes ...

Daniel Lang
07/20/2012 03:58 AM by
Daniel Lang

Amazing! Congratulations to this feature - it is really a game changer!

Ciarán O'Neill
07/20/2012 04:26 AM by
Ciarán O'Neill

This has a suspicious smell of SignalR to me :)

Ayende Rahien
07/20/2012 05:25 AM by
Ayende Rahien

Paul, Yes, it will work for embedded as well.

Ayende Rahien
07/20/2012 05:26 AM by
Ayende Rahien

Guys, This is actually NOT using SignalR. We tried using SignalR and couldn't make it (next post will discuss this in details). It is our own implementation.

Ken Egozi
07/20/2012 06:57 AM by
Ken Egozi

lol who would have guessed :)

looking fw for a post on that.

btw the SignalR gang are just around the corner from my office. ping if you want to connect

Andrej Slivko
07/20/2012 07:47 AM by
Andrej Slivko

I have one question: can i subscribe to documents that is not yet created? My use case is - 1) I'm generating guid in browser, 2) I'm sending command to create new entity with that guid to web backend 3) Web backend then sends that command to aggregate, aggregate generate events 4) View builder (that is subscribed to those events) updates web view db (raven) 5) And here i would like raven to inform my web backend about updated views and from there i will get this info to browser by signalR.

This means that i have to subscribe to raven at step 3. At that step i will have new document guid and type, but doc still won't be in raven.

Apostol
07/20/2012 09:07 AM by
Apostol

Congrats, Ayende! I get excited just by thinking how awesome is this. Having async notifications on everything on the database level - not client specific - is opening up a big number of user experience(among other) improvements one could make.

I actually thought about 6-7 ways to use this feature in just 5 minutes of thinking about it.

Good work, man!

Simone
07/20/2012 09:50 AM by
Simone

Is this feature available already in nightly builds?

Ayende Rahien
07/20/2012 09:51 AM by
Ayende Rahien

Simone, It will be later today

Brian Vallelunga
07/20/2012 07:03 PM by
Brian Vallelunga

Will there be any way to automatically tie this in with the aggressive caching? It would be great to be able to set a document to be aggressively cached until it is updated on the database.

Ayende Rahien
07/20/2012 07:12 PM by
Ayende Rahien

Sony, Define your app system user in ldap, yes.

Kele Turnipseed
07/21/2012 05:33 AM by
Kele Turnipseed

Is that using IObservable from RX? Have you considered the queryable version (IQbservable)?

Ayende Rahien
07/21/2012 07:29 AM by
Ayende Rahien

Kele, It is using IObservable, yes. You are free to use RX on top of that too.

ed
07/21/2012 01:56 PM by
ed

Ayende, what is your view on RX? In an a-sync env (in a key Silverlight app in our company) I noticed it can slice the complexity and source line count into half. Are you going to use it more ?

Ayende Rahien
07/21/2012 05:12 PM by
Ayende Rahien

Ed, I think RX is awesome, and yes, it does significantly reduces the code

Simone
07/24/2012 12:36 PM by
Simone

I have downloaded the latest unstable build but I can't see the Changes method on the document store. Is it available already?

Simone
07/24/2012 01:35 PM by
Simone

Ok sorted it out, the last unstable doesn't really seem to be the latest. Got the penultimate and it works. Nice feature!

Comments have been closed on this topic.