1

I am using Sider C# Redis client to connect to Redis server running on my windows 7 machine. https://github.com/chakrit/sider

I am able to fire set/get/select from my C# application

I now want to use the Publish/Subscribe feature so that my C# app can be informed about any changes in the redis client's "key" in an evented way (passing delegates)

I am unable to write the code for that as there are no examples on how to use the sider client page.

all i could write was this :

var client = new RedisClient(address, 6379);
string[] keys = new string[1];
keys[0] = "key1ToMonitor";
IObservable<Message<string>> obb = client.Subscribe(keys);

I know this looks lame but i got no clue how to write it in an lambda way where my function would be called if any client changes the desired keys on the redis server.

PS : I am new to this so correct me if my approach is flawed.

Edit : on adding the suggested changes i am getting the following error.

Error   7   Cannot convert lambda expression to type 'System.IObserver<Sider.Message<string>>' because it is not a delegate type    D:\_Work\TestApp\Program.cs 90  27  TestApp

the obb.subscribe signature looks like this

namespace System
{
    // Summary:
    //     Defines a provider for push-based notification.
    //
    // Type parameters:
    //   T:
    //     The object that provides notification information.This type parameter is
    //     covariant. That is, you can use either the type you specified or any type
    //     that is more derived. For more information about covariance and contravariance,
    //     see Covariance and Contravariance in Generics.
    public interface IObservable<out T>
    {
        // Summary:
        //     Notifies the provider that an observer is to receive notifications.
        //
        // Parameters:
        //   observer:
        //     The object that is to receive notifications.
        //
        // Returns:
        //     The observer's interface that enables resources to be disposed.
        IDisposable Subscribe(IObserver<T> observer);
    }
}

code :

        var client = new RedisClient(address, 6379);
        string[] keys = new string[1];
        keys[0] = "key1ToMonitor";
        IObservable<Message<string>> obb = client.Subscribe(keys);
        obb.Subscribe(x => Debug.WriteLine(x.ToString()) ); // error : doesn't let me compile  
ankur
  • 557
  • 1
  • 10
  • 37
  • This library seems to have a weird "observable" syntax, so you have to subscribe again to the `obb` to see the results. Try `obb.Subscribe(x => Debug.WriteLine(x.ToString()))` and let me know what results you get. – Enigmativity May 01 '16 at 03:25
  • Can you show in your code exactly how you tried the suggested changes? – Enigmativity May 01 '16 at 13:09
  • @Enigmativity code added – ankur May 01 '16 at 14:03
  • Try `System.Diagnostics.Debug.WriteLine`. – Enigmativity May 01 '16 at 14:21
  • error is not due to that. the error is :Error 7 Cannot convert lambda expression to type 'System.IObserver>' because it is not a delegate type D:\_Work\TestApp\Program.cs 90 27 TestApp – ankur May 01 '16 at 18:11
  • Oh, you probably don't have the right `using` statement at the top. Try adding `using System.Reactive.Linq;`. – Enigmativity May 01 '16 at 22:58
  • @Enigmativity thanks . solved. how do i close this post as answered ? – ankur May 03 '16 at 13:58
  • @Enigmativity : could you please look at this as well. http://stackoverflow.com/questions/36932701/connectionmultiplexer-connect-breaks-while-connecting-to-redis-server . I want to use a more mature C# client – ankur May 03 '16 at 14:00
  • I've added an answer with the core concepts from our discussion. You can mark that as accepted if you like. I'm sorry, but I can't help you with the other issue. I'm not familiar with Redis to diagnose. – Enigmativity May 03 '16 at 14:31

1 Answers1

0

You need to subscribe to the actual observable produced. Something like this:

obb.Subscribe(x => Debug.WriteLine(x.ToString()));

Don't forget to add using System.Reactive.Linq; to get the extensions required to convert a lambda into an observer.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172