Monthly Archives: December 2013

Fixing failing npm packages on Windows (i.e. microtime, forever)

I got into fun side project in node.js, and being Windows kid, I wanted to run it on my Windows dev machine. The thing is, it’s not that easy. Well, you can install node easily, and there is some tooling available too. The biggest PITA is that some packages are hard to build on Windows. And when you go through issues at GitHub, popular response seems to be “I don’t have any Windows box to test it and make it work” ;)

It took me several hours, but I managed to fix all the issues I had yesterday, so here’s my answer to some popular errors:

Error: Can’t find Python executable “python”

Npm (node package manager) uses build system called gyp, that runs some python scripts. It would be hard to find linux machine without python, but Windows folks often don’t have it, because they don’t need it. Me including. So install python first! They say 2.7 is good choice for gyp. And after that, add PYTHON env variable that points to your python catalog. Actually, it should point to your python.exe, because you’ll get another error I don’t remember right now (something about ENOENT).

MSBUILD : error MSB3428: Could not load the Visual C++ component “VCBuild.exe”

From what I read around Internets, this error affects Visual Studio 2013 users. It happens, because when you install node.js it has built-in node-gyp package. When you npm install node-gyp, this built-in instance is not updated, so you don’t have latest version. And sometimes it’s good to get latest version, i.e. to get VS 2013 support. This instruction helped me to update built-in node-gyp.

A little bit about tooling

I’m new to node.js and currently cannot decide what’s my favourite tooling. I’m switching between IntelliJ IDEA plugin, Node Tools for Visual Studio or running it manually from Powershell. All of them have their ups and downs. I like IntelliJ plugin, because it is stable and plays nicely. Node Tools for VS seems to have much potential, and integrate neatly with VS look and feel, but are *very* unstable (they’re still in alpha). One example – running project, where you lack some of the dependencies cause whole VS to crash. If you want to try them anyway, Scott Hanselman wrote nice introduction post.

Anyway, after fixing those build errors, node.js seems like something fun to learn. I may be covering this topic more in the future.

Using redis with F# on Windows

In one of my next projects I’ll probably use F# + redis mix. I wanted to try it out, to get some general idea how it will work. I had no previous experience with redis, and I’m still beginner in F#. I wanted to do some end-to-end example for saving and reading data. My idea was to get some Bitcoin price from external service, save it to local redis instance, read it back and chart.

In this blog post, I’ll show how I got there, and what I learned in the process.

Installing redis on Windows

Redis is in-memory key value store. It’s very fast and can store data in few data structures – strings, hashes, lists, sets and sorted sets. Official releases run on linux, but Microsoft Open Tech group developed and maintains experimental port to Windows. And this version I used for my tests.

I simply downloaded code from github, build solution in Visual Studio, and run redis-server.exe from project folder. If you want to test some commands with client, run redis-cli.exe. And if you have no idea, what are the commands try.redis.io has pretty cool interactive tutorial, that cover some basics. Running redis from code build on your dev machine probably is not the best idea for production – I would instead choose to deploy it on some linux server. But for testing purposes it’s perfect.

Getting data from external service

That part was pretty straightforward. I decided to get data from bitstamp.net, because they API for Bitcoin ticker is super easy to use. It has some limitations, but I’ll get to this later. So I made request to https://www.bitstamp.net/api/ticker/ and parsed it using Json type provider from FSharp.Data package (you can install it with Nuget). Json type provider is awesome. After you feed it with some sample data, you have all your fields just “dot” away.

https://gist.github.com/mlusiak/7901990

One thing I learned hard way in this part of code, is that in currentPrice you need those parentheses. Without them, F# will treat the whole block (lines 13 – 15) as calculating value and binding it to currentPrice – not as a function. And because values are immutable by default, it will always “return” the same price.

Saving results to redis

To access redis, I used ServiceStack’s redis client. ServiceStack is great framework to access all kinds of webservices. You can install ServiceStack.redis package using Nuget. It’s free to use up to 20 types.

https://gist.github.com/mlusiak/7902309

You can see three approaches I took (the only difference are first two lines). First one didn’t work – it saved “empty” data (empty strings, integers equal 0, etc.). As Demis Bellot explained to me on StackOverflow, StackService rely on writable properties, which immutable records in F# don’t have. He suggested to use [<CLIMutable>] attribute (which I did in third approach). In between I found this blog post, that used mutable keyword on type fields. This also works, but is not as elegant as the attribute, because it exposes those mutable fields also within your F# code.

Getting data from redis

This was very straightforward. No issues here. It just worked.

https://gist.github.com/mlusiak/7903099

Charting the data

To visualize this data, I used FSharp.Charting library. To make it work, you have to install it with Nuget and reference some libraries like System.Drawing and System.Windows.Forms. I also had to load FSharpCharting.fsx file.

https://gist.github.com/mlusiak/7903123

And it works!

Bitcoin price 11th Dec 2013 around 2am CET

Bitcoin price 11th Dec 2013 around 2am CET

Putting it all together

To summarize – getting data, saving to redis, reading back and charting – all in around 30 lines (excluding referencing libraries) of F#.

https://gist.github.com/mlusiak/7903762

Two more remarks about this code. I call Bitstamp API every 31 seconds, because it gives new data every 30 seconds anyway. If you call it earlier, you’ll get the same data as last time. Second thing is this whole #if INTERACTIVE block. I don’t know why, but when I run code in F# interactive without it, it screamed about no references to libraries. I thought referencing them in project would be enough, but no. And if I wanted to compile and run .fs file, than it failed to compile with #r clauses in it (because .fs file don’t support them, they work only in .fsx files). #if INTERACTIVE allowed me to test code in F# interactive mode, but still compile and run this code as program.

Any comments about code, and way I did things will be appreciated. I’m new to F# world, and I am happy to learn what I could do better.

Some useful references

http://try.redis.io/
http://fsharp.github.io/FSharp.Data/
http://fsharp.github.io/FSharp.Charting/
http://blogs.msdn.com/b/fsharpteam/archive/2012/07/19/more-about-fsharp-3.0-language-features.aspx
http://caxelrud.blogspot.com/2012/11/using-nosqlredis-in-windows-with-f.html