Search code examples
.neturlpush-notificationobserver-patterncouchbase-sync-gateway

how do i receive push notification from URL in desktop .net application? How do i observe a response from URL


Explanation:
Couchbase sync gateway provides changes feed sort of push notification, which basically emits change it receives. Url is looks like

localhost:4984/db/_changes?style=main_only&active_only=false&include_docs=true&doc_ids=1212&feed=normal&filter=_doc_ids

In simple terms This keep connection open between client(browser) and server(Sync gateway) and as soon as new changes are received, it notify browser the with change.

Question : I have been trying to make a .net Desktop app so i can display the changes what are being received from URL, Instead of using browser, I want to use desktop app.

Changes that are being received we would like to log/validate/transfer to some third party system.

I have tried node JS, it works; however, I could not find any approach where i can implement this in .net.

I have tried Signal R client but it has limitation and does not work.

I am trying to make a solution around observer pattern; how do i observe a URL response? Polling is not an option otherwise i would have implemented the solution.


Solution

  • I did try with this .. seems work fine.. thanks for the suggestion, this is very raw code i need to refine it for production ready.

    using (var client = new HttpClient(handler, true))
                {
                    client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
    
                    var request = new HttpRequestMessage(HttpMethod.Get, API_URL);
                    try
                    {
                        using (var response = client.SendAsync(
                        request, HttpCompletionOption.ResponseHeadersRead).Result) 
                        {
                            using (var body = await response.Content.ReadAsStreamAsync())
                            using (var reader = new StreamReader(body))
                                while (!reader.EndOfStream)
                                    Console.WriteLine(reader.ReadLine());
                        }
    
    
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
    
                }