Tuesday, October 9, 2012

ZeroMQ + MsgPack + .NET

I have created a simple testbed to demonstrate 2 .NET applications communicating with each other using ZeroMQ Publisher/Subscriber connections for the communications layer, and MsgPack as the serialization/deserialization layer.

ZeroMQ is a highly efficient and scalable network layer that acts as a reliable messaging layer on top of raw sockets. Performance is key here.

For ZeroMQ I use the x86 NuGet package available here: http://nuget.org/packages/clrzmq/2.2.5

MsgPack is a serialization library where efficient storage of the serialized data, and performance of the serialization and deserialization is the main focus.

For MsgPack I found that the current NuGet package was somewhat behind the times, so I built the dll by downloading the latest source from https://github.com/msgpack/msgpack-cli and building it in release configuration, and adding it as a reference to both the client and server applications.

With the two libraries combined, an application should be very efficient and performance should be very high. I built this testbed primarily to see how straightforward it was to use these libraries from .NET applications. As you can see, it is very straightforward indeed!

Here is the client implementation:

Here is the server implementation:

Here is the shared model:

The full solution is available here: https://github.com/SneakyBrian/ZeroMQ_MessagePack_Testbed

Friday, October 5, 2012

jQuery - Add an ajax request indicator to a page in a fixed position for debug purposes

This is a handy code snippet for adding a debug indicator to a page that uses jQuery for ajax requests. The indicator appears as a little 5px square in the top right hand corner of the page.

  • If the square is grey, then the request is in progress and has not received a response yet.
  • If the square is green then the response was received ok.
  • If the square is red then there was an error (either a timeout or an error response from the server).

XML2JSON – Command line tool for converting XML to JSON

XML2JSON is a simple command line tool for converting XML files to JSON. It uses the JSON.NET library.


XML2JSON.exe input.xml output.json

Example Input:

<?xml version="1.0"?>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<description>An in-depth look at creating applications
with XML.</description>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>

Example Output:

"catalog": {
"book": [
"id": "bk101",
"author": "Gambardella, Matthew",
"title": "XML Developer's Guide",
"genre": "Computer",
"price": "44.95",
"publish_date": "2000-10-01",
"description": "An in-depth look at creating applications \r\n with XML."
"id": "bk102",
"author": "Ralls, Kim",
"title": "Midnight Rain",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2000-12-16",
"description": "A former architect battles corporate zombies, \r\n an evil sorceress, and her own childhood to become queen \r\n of the world."


This is the code for the program:

The full .NET solution is available here: https://github.com/SneakyBrian/XML2JSON

Create A Dynamic Object From An XML File

Here are a couple of simple methods that load an xml file into a dynamic object that can be used in code. At present this code does not handle xml attributes, only elements.

Javascript - Get Query String for current page as object

Here is a code snippet that gets the current query string parameters and puts them into a query object that can then be used to read the values in subsequent code.