Design a realtime system based on SignalR

Overview: recently I worked on a realtime system related to stock trading. This post will focus on
- Self host a SignalR system in console application
- Design SignalR Hub which will push message to clients.
- Use SignalR to implement request/response pattern like a HTTP server.
Self-hosted SignalR server
There are some benefits from self-hosted SignalR server:
- In most case for controlling a classic socket server, I often need to start/stop/restart/check connection status/… of socket server. Using self host, we can easily write a command utility which will be very fast and convenience for user to interact with socket server.
- It is simple to deploy to an environment without IIS server.
- SignalR via OWIN self host server have better performance compared to run via IIS server.
Let start building our SignalR server by:
- Start new console application in Visual Studio
- using Nuget to install OwinSelfHost package
Install-Package Microsoft.AspNet.WebApi.OwinSelfHostLet start coding SignalR web application
For starting web, we also need a AppStartup which will define the way to handle CORS request and require web application to listen to SignalR protocol
We start out socket by defining address and port of the connection. Pls inserting “http” at start of address or app could not start.
We also define some parameters for our connection including ConnectionTimeout, DisconnectTimeout, KeepAlive.
That’s all and we can start application with
WebApp.Start<AppStartup>(signalRServer)and all code here
Design a message hub
We have a socket server now, let jump to second point, handle connection from client and use SignalR Hub to send message to all clients.
Here is diagram of our hub

Let look at these components:
Client: an application at client side, which will receive data pushed from server.
MyHub: a SignalR Hub handle signalr connection between client, server.
TradingPusher: This component get update trading data (in a security system), push RTMessage via SignalRSender.
SignalRSender: This component work as a gateway to decide how to send messages to clients.
First we need a Hub which will handle all connection from clients. For just pushing data from server to client, we will just declare class, no more code or method needed at the moment.
public class MyHub: Hub
{
}But we will using a SignalRSender as a gateway at server to send message to client. Here we need to reference to all current client connection. Call GetHubContext to get this.
IHubContext Context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();and in method BroadcastMessage, we will invoke method “receiveMessage” at client. You can see that I use a RTMessage (realtime message) as input, but when invoked, message will be converted to json string before sending.
Here is RTMessage
Implement Request/Reponse pattern with SignalR
It is rather often in a SignalR system, a client want to request some data from SignalR server. In this case, we can call method in hub and using push message to send message only to requester. But that pattern will request client to listen to event and decide what is message corresponding to request message. it will be make code at client side become unneeded complicated. I implemented request/response pattern which allow a client sending and receiving response like Http request/response.
For implementing request/response pattern we need to extend our hub to return message when Hub receive a request.
Also because we have many kinds of request type, I will use a gateway to route our request base on request type. But let look at method RequestData first:
in this method, we accept a RTCRequest which will be any request information from client sent to server. For example a request for list of trading in history of a stock:
var stockHistory = new RTCRequest (){ Type: "STOCK_HISTORY",
Stock: "APPL",
FromDate: "01-01-2015",
ToDate: "01-01-201"}
This method also return a RTMessage (same as message sent from TradingPusher in previous section).
So now our hub can receive a request message (structured) and build a response message and send it directly to requester (client).
Request and response with be a promise like this
MarketDataService.getStockHistory(stockHistory).then(function(result){ // handle result message in client: accessing data
if (result.Data) {
}})
Conclusion
From this post, I focus on build a self hosted signalr application in console app. Then I design two main flows of a signalR app including pushing message from server and implementing request/response pattern. This design can easily apply/extend to become a larger application.
Enjoy it!