Skip to content

Instantly share code, notes, and snippets.

@danielwertheim
Last active December 25, 2015 20:39
Show Gist options
  • Save danielwertheim/7037127 to your computer and use it in GitHub Desktop.
Save danielwertheim/7037127 to your computer and use it in GitHub Desktop.
Using MyCouch to consume CouchDb or Cloudant Changes
using System;
using System.Threading;
using MyCouch;
using MyCouch.Requests;
namespace Cloudant_Changes_Feature
{
class Program
{
static void Main(string[] args)
{
//NORMAL WILL DO A REQUEST AND BE DONE WITH IT
using (var client = new Client("http://localhost:5984/fiddles"))
{
var getChangesRequest = new GetChangesRequest { Feed = ChangesFeed.Normal };
var changes = client.Changes.GetAsync(getChangesRequest).Result;
//THE RESULT CONTAINS BUNCH OF INFO. LETS JUST PICK SEQUENCE
foreach (var row in changes.Results)
Console.WriteLine(row.Seq);
}
Console.ReadLine();
using (var client = new Client("http://localhost:5984/fiddles"))
{
var getChangesRequest = new GetChangesRequest
{
Feed = ChangesFeed.Continuous,
Since = 50, //Optional: FROM WHAT SEQUENCE DO WE WANT TO START CONSUMING
Heartbeat = 3000 //Optional: LET COUCHDB SEND A I AM ALIVE BLANK ROW EACH ms
};
var cancellation = new CancellationTokenSource();
client.Changes.GetAsync(
getChangesRequest,
data => Console.WriteLine(data),
cancellation.Token);
Console.ReadLine();
//LETS CAUSE SOME CHANGES AND SEE THEM ARIVE ASYNCHRONOUSLY
client.Documents.PostAsync("{\"value\": 1}");
client.Documents.PostAsync("{\"value\": 2}");
Console.ReadLine();
cancellation.Cancel();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment