.Net messaging pub/sub pattern

Jamie Rees

So I have an .Net application that is running on NancyFx (needs to be platform agnostic). I have a requirement that I need to send a notification when a certain event happens (subscription). The notification will implement this interface:

public interface INotification
{
    public void Notify();
}

There will be many different implementations for this e.g. Email, Pushbullet, Pushover etc.

What I am struggling with is how to actually implement this so that all implementations of INotification will execute Notify() when there is a subscription.

Can anyone point me in the right direction.

I don't really want to use any sort of external queues, since the application will be running on the users machines.

So the application is a self-hosted, the client and the server are located on the users machine and it does not talk to the outside world, it's all internal.

quetzalcoatl

Excuse me for a second, I'll just want to make sure I got you right. You have not written what's the exact problem, so I assume you are stuck somewhere at some basics. I'll also ignore NancyFX, I don't know it, I assume that if it supported this case, you'd find it in docs.

To let the client know that something has changed, you essentially have only 3 options:
(a) there's a list of old events on the server, server updates it when events happen, clients periodically read them and invoke their 'Notify' that does Ping!
(b) each client opens a connection to the server and keeps it open, server remembers the connections and when a new even happens, sends a message to all clients over already-opened connections, clients get the message and invoke their 'Notify' that does Ping!
(c) clients send registration message to the server, it contains callback address/port/etc, server stores that. when event occurs, server reads the list and sends infos to these addresses, clients handle these requests and call their Notify according to the request

Since you use words 'publish-subscribe', I assume you meant (C).

For this option, regarding notifications, the roles of client/server are actually reversed. In a normal case, the server exposes API to client, client connects to it, sends a request, server handles it and returns some response. Here, both sides must do the same. The client app must also expose a callable API to the server, so that when "event" occurs, the server can connect to the client's API and send him a request with notification data.

Now, how you'd structure the API - it's up to you. You can have one Notify(string xmlizedOrJsonizedData) method, you can have parameters Notify(string infotype, datetime, data), or many methods NotifyEmal(...) NotifyBullet(...) ... - after implementing the registration and subscription bookkeeping, you will just need the server to call into the client's api with correct request data, just like the client does the same all the time.

Now that's a bit of work to write all of that and reinvent the wheel. There are lots of libraries that already can do it. I looked and I didn't find anything in NancyFx docs about that. Probably you could use it to create a client-side api, just like you create server-side api, but .. there's a catch.

Client-side and server-side differ.

When client talks to sever, there is only one server to send to and to listen to. You can do it in naive way, even blocking the UI if you like. When server sends notifications back, there can be 10000s of clients. You should not even start writing it in a naive way. Looping over that many clients and waiting till finished can freeze your server completely, and if not freeze, then cause slowdowns and timeouts. Furthermore, server is out there in public. Clients are not. Clients often are behind NATs, firewalls, and all other funny things that pass traffic from Client->to->Server but may block traffic in the other direction. Most basic example is blocking ports. :80 on the server is almost always passed through firewalls, but :80 on client will be probably unavailable and when client opens its api on :23122 it probably won't be configured on their firewalls/routers .. unless you handle that with upnp/etc.

That's why it's good to pick a library that can do all of that for you.. Sorry, I can't recall any name now, check i.e. google for PublishSubscribe pattern, or client-notification or push-notification server-side library.

That's why a thing called "WebSockets" was invented. That's essentially option (B) I talked about in the beginning. It's really worth investigating. The whole concept of subscribe-publish is also implementable as calls/responses to server/client apis/interfaces over websocket, but it saves you a lot of work and network problems.

I've found an info that it's possible to use Nancy and SignalR so that may be a very good start.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bridge Pattern applied to messaging API

Design pattern for getting initial value in pubsub in autobahn

Strategy pattern and Android Service/UI Messaging communication

Dataflow Template/Pattern in enriching fixed BigQuery data by streaming Pubsub data

How to have variable callback types based on an input parameter in a PubSub pattern?

MVC Mapping between View Model and Request-Response messaging pattern

spring-cloud-stream request-reply messaging pattern

Aeron Messaging Pattern for Pub/Sub, One Way and Competing Consumer

Is there a messaging system for .NET+Mono with no external dependency?

A .NET EF Repository pattern

.NET Regex not finding pattern

Flux pattern for .net

Regex Pattern Matching .Net

MVP pattern on .net

.NET DDD Domain Pattern with Grandchildren

Pattern For .NET Regex Replace Method

Pattern Matching in Confluent.net

How can i change following interface to accept parameters for different types of messaging system. any design pattern ?

Which messaging service to work with Azure Function Fan-out pattern without fan-back?

What messaging pattern should I use to process and return responses from a rest request?

ZeroMQ, can we use inproc: transport along with pub/sub messaging pattern

RegEx for matching a special pattern in VB.net

Regex Style Pattern Matching in .NET Object Lists

Subscribing to Events in C#.NET MVC pattern

Find pattern in json with json.net and linq

VB.NET Select String Matching Pattern

Generic Repository pattern for .net core with Dapper

How to implement a correct repository pattern in .NET

Strategy Pattern with ASP.Net Core DI

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive