I was wondering if there was an established pattern for doing what I'm trying to achieve. It seems a fairly common scenario so I'd be surprised if there wasn't, however I'm unable to find anything. The main technologies I'm using:
ASP.NET MVC4
SignalR
Redis (using the ServiceStack API)
I would like to subscribe to messages from Redis and publish these to end-users using SignalR.
Rather than setting up a new Redis connection for each end-user, I would like to maintain one subscription per topic/channel. Each topic would then have its own SignalR hub, which the end-users would subsequently subscribe to. So essentially redis topic --> signalR hub --> many users.
My questions revolves around how to maintain these subscriptions. They are (by nature) blocking, which is a problem when dealing with web applications. Is it safe for me to simply create a separate thread to handle each topic's subscription, or is there a better way of doing this? Or have I misinterpreted the problem entirely?
I'm fairly new to web development, so any thoughts would be appreciated.
Related
I would like to find a solution to create a pub/sub medium for 2 microservices to talk to each other,
I am aware i can use some third parties E.g Redis, RabbitMQ
Implementing event-based communication between microservices (integration events)
The challenge lies on the client is unable to allow install any third parties tool due to security reason.
The messageQueue server in Windows won't be allowed to use too.
I can only use the applications that is only existed in the server.
Therefore i am asking if there is anyway that i can create one simple app using windows service.
It is a one-to-many relationship. I have one service that will be dealing with data, once if there is any update, it will publish to those services that is subsribed to it.
It seems my problem could be similar with
.NET Scalable Pub/Sub service implementation
WCF Pub/Sub with subscriber caching(link is dead on the WCF pub-sub)
but i dont see any critical solutions.
I was thinking to use data notifications that MSSQL offers as last alternatives, but seems like it could cause a bottle neck when the applications get scale up.
The internet is so much flooded with articles using third parties tool.
Thanks
Check out Rebus library, that allows using different transport methods to send end receive messages in just a line of code (so in the future you can change it without effort).
You could use SQL Server or try to develop your own transport method
I have a chat SignalR server, the chat support group chatting.
I also have a server which actually creates the groups and other group managment tools.
Whenever a user leaves a group (via http post ) to server, I want the chat service to trigger some methods, such as LeaveGroup and some other logictics.
I bound the connectionId to userId so I got the parsing request covered.
QUESTION IS: What is the best practice of communication between server/service and the signalr server.
Taking in mind, I dont want to compormise on scalability on each of my servers/services.
My idea is more or less host a web api server inside the SignalR server, but I can't seem to find any topics suggesting that could damage the performance.
Ideas?
Thanks alot.
p.s
I know that there is no code involved in here. but it seems irrelevant. I have self hosted web api in a window service I have, so the code is pretty much the same.
I would love to provide more data/information if thats neccesary
It seems like this documentation is most applicable to what you're trying to do: https://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr
It speaks specifically about how to communicate from your server/service application to the signalr clients. Communicating from the client to the server/service could be done either through the signalr hub, or with other web API.
From a best practice perspective, the documentation specifically states (https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub):
If you need to use the context multiple-times in a long-lived object,
get the reference once and save it rather than getting it again each
time. Getting the context once ensures that SignalR sends messages to
clients in the same sequence in which your Hub methods make client
method invocations. For a tutorial that shows how to use the SignalR
context for a Hub, see Server Broadcast with ASP.NET SignalR.
If you're really into scalability, you might want to look into integrating your signalr communications into some other message queueing system, but that's probably overkill for most circumstances.
In a previous solution, I recall setting up code-behind-type hub connections for unit testing. I believe I used code from the previous SignalR Hubs C# Client GitHub wiki.
In the SignalR Hubs API guide there is an example of using a console-based client:
stockTickerHubProxyProxy.On("Notify", () => Console.WriteLine("Notified!"));
In an upcoming application there will be notifications to be pushed to several clients. Some of these clients will be UI-based (JS), and others, will be some back-end code that will perform some task.
I am familiar with the JS-based client connectivity. My question centers around attaching code-behind-type clients, and how their methods "notified".
I understand that with a UI-based client, the mechanism is to push the update to the client through dynamic invocations to client-side "methods". Since it's been a few months since I've touched SignalR, what is the best way to accomplish this where there is no UI involved?
To verify that I am understanding the documentation correctly , when valid transport method is used, a C#-based (non UI/JS) client is notified (push) through the SignalR framework - no client polling required?
The essential mechanism is the same for JS and C# clients. The solution is exactly what you mentioned in your question. The 'On' method will subscribe the client to any "notify event" triggered by the server. You can refer to the documentation to get a better understanding:
http://www.asp.net/signalr
I am creating a new ASP MVC order application in the Amazon (AWS) cloud with the persistence layer at my local datacenter. I will be using the CQRS pattern. The goal of the project is high availability using Queue(s) to store and forward writes (commands/events) that can be picked up and handled asynchronously at my local datacenter. Then, ff the WAN or my local datacenter fails, my cloud MVC app can still take orders and just queue them up until processing can resume.
My first thought was to use AWS SQS for the queuing and create my own queue consumer/dispatcher/handler in my own c# application to process the incoming messages/events.
MVC (# Amazon) --> Event/POCO --> SQS --> QueueReader (# my datacenter) --> DB
Then I found NServiceBus. NSB seems to handle lots of details very nicely: message handling, retries, error handling, etc. I hate to reinvent the wheel, and NServiceBus seems like a full featured and mature product that would be perfect for me.
But on further research, it does NOT look like NServiceBus is really meant to be used over the WAN in physically separated environments (Cloud to my Datacenter). Google and SO don't really paint a good picture of using NServiceBus across the WAN like I need.
Can I do this?
MVC (# Amazon) --> Event/POCO --> NServiceBus over WAN --> NServiceBus Handler(s) --> DB
How can I use NServiceBus across the WAN? Or is there a better solution to handle queuing and message handling between Amazon an my local datacenter?
Using SQS as a transport for NServiceBus is an option, however you have to be aware of the trade offs as described here. This has been done with Azure queue storage, though I'm not aware of any great SQS implementations.
Another option is to create a VPN between your datacenter and an AWS VPC. This would allow direct MSMQ communication between AWS servers and your data center, provided you open the appropriate ports in the corresponding security group. There are some caveats with this approach. First, is regarding endpoint names. NServiceBus version 2.6 and below uses Environment.MachineName as the name of the endpoint, for which you would have to setup a proper DNS. I believe later versions use the machine's IP address. Perhaps a more important caveat is that a VPN makes your systems more coupled.
Yet another way, is to use the NServiceBus notion of a gateway. This however should be a logical business decision. A gateway is very similar to the regular transport but is usually has a different business context behind it.
NServiceBus includes a Gateway component that handles bridging physically separated data centers.
http://docs.particular.net/nservicebus/gateway/
It basically moves the messaging to an HTTP channel and handles the retry logic and deduplication issues that you'd normally have with a web service.
If you download the full NServiceBus package (not just include it via NuGet) then you will see a folder full of samples and one of those covers usage of the Gateway, and that is a great way to get started.
I have been playing around with pubsub and so far it looks good for what I need (a basic game experiment).
From a Javascript perspective and mobile (via Appcelerator's Titanium) I can really see the value of using pubsub.
However, I need to write a server app in c#/.NET (although open to other ideas) to listen to the subscriber queue I have, and process the messages.. which involves some decision making etc, and then possibly writing another message to the publish queue for example.
So far I have played with RX (Reactive Extensions) for C# which listen on my subscribe channel. So far so good, I see the messages come in, although for now I just wrote a C# console app to test.
My question is would the best way to wait and listen for pubsub subscriber messages be to write a windows service app? or is there another technique more appropriate? obviously at some possible point I might have to scale the server to 2-3 servers, however given the nature of pubsub queue/messaging, I don't see a problem if I had some load-balancing etc.
Any ideas welcome!
Use Service Bus. When cloud is good for you, than Azure Service Bus. When not then nServiceBus. Take a look also for RabbitMQ, it's AMQP framework and is able to do more then pubsub. Also rabbit has multiple clients on multiple platorms. For example one of approaches purely for JavaScript is RabitMQ + Node.js + WebSockets.
All clients and devtools, and articles about RabbitMQ for different platforms and languages are here.
There is also special RabbitMQ binding for .NET, find it here.
NServiceBus PubSub explanation is here. It's .NET service bus, but is not such free as RabbitMQ. Anyway RabbitMQ is platform agnostic.
Any of service buses implementations already has PubSub, that is the reason they exist. Therefore there is no reason to implement, what is already implemented