Skip to content

EasyRedisMQ Chat application example #3

@yourmailhacked

Description

@yourmailhacked

I'm using EasyRedisMQ for message queue. It works well for MQ purpose, but I would like to use EasyRedisMQ for Chat also. Problem-> When publisher is publishing message to all subscribers, But i want to publish to only single subscriber.

// redis config
var redisConfiguration = new RedisConfiguration()
{
AbortOnConnectFail = true,
KeyPrefix = "my_key_prefix",
Hosts = new RedisHost[]
{
new RedisHost(){Host = "localhost", Port = 6379},
},
AllowAdmin = true,
ConnectTimeout = 3000,
Database = 1,
ServerEnumerationStrategy = new ServerEnumerationStrategy()
{
Mode = ServerEnumerationStrategy.ModeOptions.All,
TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
}
};

// simple injector 
    var container = new Container();
    RegisterAllMq(container);
    container.Register<ISerializer>(()=> new NewtonsoftSerializer(new JsonSerializerSettings(){Culture = CultureInfo.CurrentCulture}));

    //ISerializer searilizer =;
    container.Register<ICacheClient>(
        () => new StackExchangeRedisCacheClient(container.GetInstance<ISerializer>(), redisConfiguration), Lifestyle.Singleton);


    var messageBroker = container.GetInstance<IMessageBroker>();

    //The subscriber has it's own id (like login id)
    //This consumer will get its own queue and it gets its own copy of every message published.
    Console.WriteLine("Enter you user id");
    var id = Console.ReadLine();
    var subscriber = messageBroker.SubscribeAsync<ConsoleMessage>(id, async x => { await WriteConsoleMessageAsync(x); });

// subscribed for this id. when multiple console are opened, each client will have different id's. ex. console id 1, console id 2 ...

    Console.WriteLine($"{id} ==> is listening for messages. Ctrl+C to quit.");

// this is handling publishing of messages, this code is also part of same console app. it is publishing message to all console 1,2,3. while (true) { Console.WriteLine("Enter message to be published. Ctrl+C to quit."); var message = Console.ReadLine();

messageBroker.PublishAsync(new ConsoleMessage
{
Message = message
});
Console.WriteLine("Message published successfully.");

But I would like to achieve, something like one 2 one chat app. subscriber 'A' will enter his id(111) and he will also provide another subscriber 'B' id (222) (to whom he want to send message). So when subscriber 'A' sends message, it should only publish to Subscriber 'B'.

I know in this example, I don't have option to enter subscriber 'B' id option.

goal is to use redis for chat with pure c# (no angulr, no sockets nothing)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions