Tag Archives: socket.io

Connecting a node app using socket.io to a Windows Forms .NET client application

In previous posts, I developed a node.js application that could provide temperature updates to clients using socket.io. With renewed interest in .NET, I thought it would be interesting to see if I couldn’t build a Windows Form app client that could connect to socket.io interface and receive the temperature updates.

Construct a .NET App which Supports a Socket.io Connection

While targeting a Universal Windows project has perhaps more contemporary relevance, for this example, we’ll stick to the classic Windows Form application.

You’ll want to start a new Windows Forms Application, as selected from Visual C# – Windows – Classic Desktop template section.

Classic Desktop Windows Form Application

Classic Desktop – Windows Forms Application

After we’ve started a new project, we’ll need to add two nuget extensions: SocketIoClientDotNet and Json.NET by NewtonSoft.

SocketIoClientDotNet provides a .NET-based socket.io implementation, in this case the client-side interface – a server-side interface is also available. Use nuget to search out and install the library.

Nuget SocketIoClient Library

Install SocketIoClientDotNet from nuget

We also need to also nuget Json.NET, which will provide us tools to handle incoming socket.io data delivered in JSON format.

Nuget NewtonSoft Json.NET.jpg

Install NewtonSoft’s Json.NET from nuget

With the two extensions installed, we’ll want to go ahead and build out the basic UI in a Windows Form. For this example, we’ll want to press a button to initiate our socket.io client, find out if we connected (putting the status in a TextBox) and then, if connected, display the temperature our node app will be sending us (within another TextBox):

Our Windows Form Application

The SocketIoClient’s Windows Form

While there are better ways to handle the connection, for the sake of simplicity, I’ve put the grunt work within the socketIoManager method – which aside from instantiating the socket.io connection, provides the basic socket.io On handlers to manage incoming events/data. About the only other thing mentioning is that any UI updates need to be managed by delegates because events received by the socketIoManager method are happening on a different thread than the UI thread, hence the need for the UpdateStatus and UpdateTemp methods:

Initialize and Handle Socket.io

Here’s a short video explaining the project:

 

 

 

 

Project: BBB Admin Page – Setting up a Development Environment

To summarize our current location: We’re on the path of building an administration website built using the AngularJS framework, hosted on the BeagleBone Black (BBB). The purpose of the administration site is to allow us to adjust key configuration parameters and launch other applications. Essentially, I want to provide a web interface to the BBB, similar to the one provided for a network router.

Having written a prototype node.js app which provides us near real-time data via the sockets.io module, we are ready to begin building out the application to do more. Because we’re going to do a lot more, we need to better define our development environment.

In this post, I’ll document the development environment I’m using to build out the administration web application then in subsequent posts I’ll delve into discussions on our AngularJS MV* infrastructure and tying Angular to core BBB functions.

Visual Studio: A Most Unconventional Development Environment for BBB

I’m going to use Visual Studio Community Edition 2013 (VS2013) as my development environment. It may seem unconventional because my target platform is the BBB, but since a lot of the code I’m going to be writing is web-oriented and the bulk of the coding is most easily done on my Windows PC, I need a Windows development environment. While text editors (e.g., Notepad++, EditPlus) and newer development tools (e.g., Sublime, JetBrainsBrackets) are possible options, I’m pretty used to the Visual Studio toolset. The new VS2013 is a full-featured professional package, so there’s a ton of functionality available, including Javascript Intellisense and debugging capabilities. Microsoft has put a focus on providing functionality for JavaScript, Bootstrap and AngularJS developers, primarily for websites running on their Azure platforms, but we can leverage these tools for our work on the BBB – we’ll simply publish to BBB instead of Azure. Best of all, Microsoft has made Visual Studio Community Edition free for non-enterprise use.

So let’s start. If you don’t have VS2013 already, head to the the Visual Studio website and download it. It’s not a small piece of software, so allow 15-30 minutes to download and install.

Microsoft Visual Studio Community 2013 Website

Microsoft Visual Studio Community 2013 Website

Before we jump in, I’m also going to add in the Web Essentials 2013 for Visual Studio plugin to give us even more developer friendly tools. Information on Web Essentials is available here, but the download is kept here. Once everything is installed, we’re ready to fire up Visual Studio.

Visual Studio Community Edition

Visual Studio Community Edition

Creating a VS2013 node.js Project

To start, we’ll create a project, but reference our existing work, then we’ll import the relevant files to get to a point where we are ready to work.

Create Visual Studio Node.js Project

Step A – Create Visual Studio Node.js Project

Import Files into Visual Studio 2013

Step B – Import Files into Visual Studio 2013

Up and Running in Visual Studio with node.js Project

Step C – Up and Running in Visual Studio with node.js Project

Preparing the VS2013 for Debugging: Reconciling the Required Server Modules

If we try debugging, by pressing the green arrow, we’ll run into two problems:

  • Visual Studio will ask us to build the project. VS2013 is not really compiling, but it needs to build a framework for it to support our debugging efforts. This is pretty easy to get around, we just tell VS2013 OK, do what you need to do.
  • Our application will fail in the command line, because we’re missing our referenced libraries. To fix this, we need to import the modules into our project.

Specifically, we need the following modules for the server:

  • http, fs – since these are included as part of the base node installation, we do not need to install them
  • socket.io – we will import this module, using VS2013’s npm package installation tool
  • bonescript – we will modify our code to not require this, using node’s fs module instead

We can install socket.io by selecting npm (Node Package Manager) from the Solution Explorer window, right-clicking and selecting the “Install New npm Packages …” option. The first time you do this, it will download all of the available module options (there are a ton), but subsequent times, it will be near instantaneous to get to the window we want.

VS2013 - Install npm Packages

VS2013 – Install npm Packages

As per our first few lines of the app.js file, we can see the list of modules we need to include. Simply search for the relevant modules and add them, Visual Studio will add them to the project and, optionally, to the package.json file. Make sure that you include the right modules, because there are so many of the same name – if you make a mistake, simply remove them from the Solution Explorer tree.

VS2013 - Install npm Module Dialog

VS2013 – Install npm Module Dialog

We will also modify our code to use the fs module, instead of the bonescript module, to have a more generic code base.

Running and Debugging our Application

With this complete, we’re ready to test our project. We can select the Debug menu’s “Start debugging” option, press green arrow button on the menu bar or simply press the F5 function key to launch the application. Of course, we won’t see a temperature, because we’re running the application on a PC for the moment, but it will prove that our application is functioning and ready for debugging. We can even use breakpoints and the watch windows if need be.

VS2013 - Project Debugging

VS2013 – Project Debugging

Adding Some Polish to the Client-Side Presentation Through the Bootstrap.js Module

Bootstrap is a widely-used Javascript module for marking up client-side user interface. It’s popularity is because it is a mobile-first, responsive HTML/CSS/Javascript framework.

To add Bootstrap to our client HTML, we’ll first have to reference it within a script. For simplicity-sake, we’ll use the provided CDN in our HTML header. We can now add some basic commands to make some improvements to our previous gaudy html. Specifically, we’ll throw in a couple of labels to pretty our browser presentation:

Adding Some Basic Bootstrap to App Client

Adding Some Basic Bootstrap to our App Client

Running App and Client (with Bootstrap)

Running App and Client (with Bootstrap)

Final Step – Upload to the BeagleBone Black

There are two way to manage how our code now gets to the BBB:

  • Use GitHub – in this case you’ll add, commit and push the changes to the repository, then pull them down from the BBB. This is the recommended approach
  • Use WinSCP – upload the files to BBB directly. This may be faster and more direct. Furthermore, it is possible to set up a directory synchronization task to manage host and target directories automatically.

For this blog entry we will show the second option, using WinSCP’s synchronization functionality. In this method, you’ll might want to omit synchronizing the VS2013 directories and files, as shown below:

Using WinSCP Synchronize Directories Feature

Using WinSCP Synchronize Directories Feature

We can PuTTY to the BBB and run our application:

Updated App  Running on BBB

Finally, I’ve updated the repository here: https://github.com/bitstobrowser/AdminApp

Project: BBB Admin Page – Connecting a BeagleBone node.js app to a browser using socket.io (Part 2)

So, three things to improve on the last post:

First, I’ve created a github repository which contains the source for our project. It can be found here: https://github.com/bitstobrowser/socketTemp.

To run the code, you can download it either directly to your BeagleBone Black or to your desktop and use a utility, like WinSCP, to transfer it to your BBB. Once on your BBB, navigate to the directory and start the server by executing the following command: node server.js.

Second, let’s quickly review the code to get an understanding of how it works:

BBB Temperature Server

BBB Temperature Server

  • Lines 3-6: Define the set of libraries that we need to use. Importantly, we tell socket.io to listen for any connections on the server’s port.
  • Line 8: Here we listen for any HTTP requests on port 8888.
  • Lines 10-21: Serve up a client.html file to all browser HTTP requests
  • Line 23: Sets a timer to go off every 2500ms or 2.5 seconds. We’ll use this to read and update the attached browsers.
  • Lines 25-26: Is the user file which contains the temperature information
  • Lines 28-32: This is the function that is called every 2500ms and it reads the contents of the temperature file and then calls another function to send the information out to any clients.
  • Lines 34-39: This puts the temperature in a JSON object and then emits (sends) the data to all attached socket connections.

At the client end, it’s even less lines of code:

BBB Socket Temp Client

BBB Socket Temp Client

  • Lines 3-4: We specify the JavaScript libraries that we will need to reference.
  • Line 6: Creates a socket connection, using the same port as the specified in the browser access.
  • Line 7: Defines what we do in case we get a socket message of type ‘temperature’ – we will call the handleTemperature function.
  • Lines 9-14: When we receive a ‘temperature’ function, we unpack the JSON object to get the value sent to us. We convert to Fahrenheit for our display. Then finally on line 13, we update the HTML as marked by the “temp” ID with the latest temperature information.
  • Lines 18-22: This is our basic HTML display – line 22 will be adjusted whenever we receive a message by the handleTemperature function.

Third, I’ve put a better version of the video up – much better resolution:

Project: BBB Admin Page – Connecting a BeagleBone node.js app to a browser using socket.io

Having established running a basic node.js application on the BeagleBone Black in the previous post, we can now progress to connecting our app to a browser. We’ll do this by running JavaScript on both the browser and our BBB, then allow them to share data in real-time using the socket.io library.

What we’ll do in this step is to read the on-board CPU temperature of the ARM 3359 processor, every second and inform any attached browser of the most recent value. In an obscure forum discussion, I was able to find out my Debian BBB-specific distro makes this CPU temperature register location available at the /sys/devices/ocp.3/44e10448.bandgap/temp1_input you may need to verify that this is available on your OS or if there is a slight variation of the location. Information on the bandgap register is available in the AM335x Technical Reference Manual (chapter 9). The temp_input file holds the current temperature (multiplied by 1000) – for example, in the image below, the CPU temperature is 53 C. On the web, you’ll see many caveats that this not an accurate measurement and should not be used for any serious application – but for our demo app, it serves the purpose of sending I/O changes from the BBB to any attached browser.

Location of CPU Temperature Information

Location of CPU Temperature Information

Our demo consists of two parts: the node.js server located on the BBB and a web page with a JavaScript connection to the BBB.

Node.Js CPU Temperature Server

Our node.js server does two things, first it serves up a web page, when requested, and secondly, every second it sends the latest temperature to all of the attached browsers.

Our first step to getting this working is to install socket.io using the node package manager (npm), which handles the transporting of temperature information to the browser.

Install socket.io using npm

Install socket.io using npm

Unexpected Detour to Fix Disk Size

I noticed that I was having problems doing any work, node started behaving weird and I started getting errors. I realized that the partition allocated for the Linux kernel on my SD card was full. While I had an 8GB SD lying around, I was able to stumble on this excellent set of instructions to expand the Linux kernel partition on my SD card, which allows me to continue to use my 4GB SD card, with a 10 minute procedure.

Here’s what my SD card’s Linux kernel partition looked like before:

Linux kernel partition on SD before

Linux kernel partition on SD before

Here’s the disk allocation after expanding the partition size:

Linux kernel partition on SD after

Linux kernel partition on SD after

And finally, after finishing up the resizing command:

Result of Linux kernel paritition resize

Result of Linux kernel partition resize

What I learned from this detour is that a 2GB SD card shouldn’t be used for Debian on BBB, the 4GB is fine, but consider right-sizing the partitions beforehand and, if available, start with a 8GB SD.

Temperature Sensor App

So back from our tangent, we’re ready to test out reading the on-board temperature measurement and displaying it in (near) real-time to a connected browser. I’ve written a very small node.js app called bbbTemp which performs this function.

I found a very useful utility, called WinSCP, to allow me to transfer files to the BBB from my Windows machine. WinSCP gives me the luxury of doing development and testing in Windows, then fine tuning and minor debugging on the BBB. (It’s one of the nice little tools that deserves a donation – I gave mine and I hope that you will too.)

WinSCP File Transfer

WinSCP File Transfer

In next week’s post, I’ll share the code, but in the meantime, here’s a video of our application running: