F# and SignalR Stock Ticker: Part 2

Following up on my prior post found here about using F# to write the Stock Ticker example found on SignalR’s website, I went to implement the heart of the application – the stock ticker class.

The original C# class suffers from a violation of command/query separation and also does a couple of things.  Breaking out the code functionality, the class creates a list of random stocks in the constructor. 

image

Then there is a timer that loops and periodically updates the current stock price. 

image

Finally, it broadcasts the new stock price to any connected clients.

image

Because the class depends on the clients for its creation and lifetime, it implements the singleton pattern – you access the class via its Instance property.  This is a very common pattern:

  1. //Singleton instance
  2. private readonly static Lazy<StockTicker> _instance =
  3.     new Lazy<StockTicker>(() =>
  4.         new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));

  1. public static StockTicker Instance
  2. {
  3.     get
  4.     {
  5.         return _instance.Value;
  6.     }
  7. }

Attacking the class from a F# point of view, I first addressed the singleton pattern.  I checked out the Singleton pattern in Liu’s F# for C# Developers. The sentience that caught my eye was “An F# value is immutable by default, and this guarantees there is only on instance.” (p149)  Liu then goes and builds an example using a private class and shows how to reference it via a Instance method.  My take-away from the example is that you don’t need a Singleton pattern in F# – because everything is Singleton by default.  Another way to look at it is that a Singleton pattern is a well-accepted workaround the limitations that mutability brings when using C#.

I then jumped over to the updating stock prices – after all, how can you send out a list of new stock prices if you can’t mutate the list or the individual stocks within the list?  Quite easily, in fact.

The first thing I did was to create a StockTicker class that takes in a SignalR HubContext and a list of stocks.

  1. type StockTicker(clients: IHubConnectionContext, stocks: IEnumerable<Stock>) = class

I then added the logic to update the list and stocks.

  1. let rangePercent = 0.002
  2. let updateInterval = TimeSpan.FromMilliseconds(250.)
  3. let updateStockPrice stock:Stock =
  4.     let updateOrNotRandom = new Random()
  5.     let r = updateOrNotRandom.NextDouble();
  6.     match r with
  7.         | r when r <= 1. -> stock
  8.         | _ ->
  9.  
  10.             let random = new Random(int(Math.Floor(stock.Price)))
  11.             let percentChange = random.NextDouble() * rangePercent
  12.             let pos = random.NextDouble() > 0.51
  13.             let change = Math.Round(stock.Price * decimal(percentChange),2)
  14.             let newPrice = stock.Price + change
  15.             new Stock(stock.Symbol, stock.DayOpen, newPrice)
  16. let updatedStocks = stocks
  17.                         |> Seq.map(fun stock -> updateStockPrice(stock))

Looking at the code, the word “update” in the prior sentence is wrong.  I am not updating anything.  I am replacing the list and the stocks with the new price (if determined).  Who needs a singleton?  F# doesn’t.

I then attempted to notify the clients like so:

  1. member x.Clients = clients
  2. member x.Stocks = stocks
  3. member x.BroadcastStockPrice (stock: Stock) =
  4.     x.Clients.All.updateStockPrice(stock)

 

But I got a red squiggly line of approbation (RSLA) on the updateStockPrice method.  The compiler is complaining that

Error    1    The field, constructor or member ‘updateStockPrice’ is not defined   

And reading the SignalR explanation here:

The updateStockPrice method that you are calling in BroadcastStockPrice doesn’t exist yet; you’ll add it later when you write code that runs on the client. You can refer to updateStockPrice here because Clients.All is dynamic, which means the expression will be evaluated at runtime

So how does F# accommodate the dynamic nature of Clients.All?  I don’t know so off to StackOverflow I go….

In any event, I can then wire up a method that broadcasts the new stock prices like so:

  1. member x.BroadcastAllPrices =
  2.     x.Clients.All.updateAllStockPrices(updatedStocks)

And then write a method that calls this broadcast method every quarter second:

  1.  member x.Start =
  2.      async {
  3.             while true do
  4.              do! Async.Sleep updateInterval
  5.              x.BroadcastAllPrices
  6.      } |> Async.StartImmediate

Note that I tried to figure out the Timer class and subsequent Event for the timer, but I couldn’t.  I stumbled upon this post to ditch the timer in favor of the code above and since it works, I am all for it.  Figuring out events in F# is a battle for another day…

 

 

 

Advertisements

One Response to F# and SignalR Stock Ticker: Part 2

  1. Pingback: F# Weekly #49, 2013 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: