“Hello World” for .NET with Lightstreamer
(Originally published on cometdaily.com)This article will focus on a .NET port of the Java Data Adapter that we illustrated in "Hello World" for Lightstreamer. In particular, both a C# version and a Visual Basic version of the Data Adapter will be shown.
Let’s get started
First, please take a look at the previous installment, which provides some background and the general description of the application. Notice that the front-end will be exactly the same. We created a very simple HTML page that subscribes to the “greetings” item, using the “HELLOWORLD” Adapter. Now, we will replace the “HELLOWORLD” Adapter implementation based on Java with C# and Visual Basic equivalents. On the client side, nothing will change, as server side Adapters can be transparently switched and changed, as long as they respect the same interfaces. Thanks to this decoupling provided by Lightstreamer Server, we could even do something different. For example, we could keep the Java Adapter on the server side and use Flex, instead of HTML, on the client side. Or we could use the C# Adapter on the server side and use Java, instead of HMTL or Flex, on the client side. Basically, all the combinations of languages and technologies on the client side and on the server side are supported.
Give me some .NET interfaces!
Lightstreamer Server is written in Java, so its native Adapter interfaces are Java based. The .NET interfaces are added through the Lightstreamer Adapter Remoting Infrastructure (ARI). Let’s have a look at it.

ARI is simply made up of two Proxy Adapters and a Network Protocol. The two Proxy Adapters implement the Java interfaces and are meant to be plugged into Lightstreamer Kernel, exactly as we did for our original “HELLOWORLD” Java Adapter. There are two Proxy Adapters because one implements the Data Adapter interface and the other implements the Metadata Adapter interface. Our “Hello World” example uses a default Metadata Adapter, so we only need the Proxy Data Adapter.
What does the Proxy Data Adapter do? Basically, it exposes the Data Adapter interface through TCP sockets. In other words, it offers a Network Protocol, which any remote counterpart can implement to behave as a Lightstreamer Data Adapter. This means you can write a remote Data Adapter in C, in PHP, or in COBOL, provided that you have access to very standard TCP sockets.
But—here is some magic—if your remote Data Adapter is based on .NET, you can forget about direct socket programming, and leverage a ready-made library that exposes a higher level .NET interface. So, you will simply have to implement this .NET interface. Ok, let’s recap… The Proxy Data Adapter converts from a Java interface to TCP sockets. The .NET library converts from TCP sockets to a .NET interface. Clear enough?
Creating the C# Data Adapter
Let’s start with C#. Feel free to skip this section if you are only interested in the Visual Basic example (I will replicate the same comments on the code).
We should implement two classes. One (which we will call DataAdapterLauncher) contains the application’s Main and initializes the DataProviderServer (the provided piece of code that implements the Network Protocol). The other (which we will call HelloWorldAdapter) implements the actual Adapter interface.
- Create a new C# project (I used Microsoft’s Visual C# 2008 Express Edition).
- From the “New Project…” wizard, choose the “Console Application” template. Let’s use “adapter_csharp” as the project name.
- From the “Solution Explorer”, delete the default Program.cs, then add a reference to the Lightstreamer .NET library: go to the “Browse” tab of the “Add Reference” dialog and point to the DotNetAdapter_N2.dll file, which you can find in the “Lightstreamer\DOCS-SDKs\sdk_adapter_dotnet\lib\dotnet_2.0″ folder of your Lightstreamer installation.
DataAdapterLauncher
Add a new class (let’s call its file DataAdapterLauncher.cs). Replace the source code with this:
1 |
using System; |
This code creates a DataProviderServer instance and assigns a HelloWorldAdapter instance (which we will define below) to it. Then, it creates two TCP client sockets, because the Proxy Data Adapter, to which our remote .NET Adapter will connect, needs two connections (but as I said, after creating these sockets, you don’t have to bother with reading and writing, as these operations are automatically handled by the DataProviderServer). Let’s use TCP ports 6661 and 6662. Assign the stream of the first socket to the RequestStream and ReplyStream properties of the DataProviderServer. Assign the stream of the second socket to the NotifyStream property of the DataProviderServer. Finally, you start the DataProviderServer.
HelloWorldAdapter
Now add another class (call its file HelloWorld.cs). Replace the source code with this:
1 |
using System; |
The HelloWorldAdapter class implements the IDataProvider interface (which is the .NET remote equivalent of the Java DataProvider interface).
Implement the SetListener method to receive a reference to the server’s listener that you will use to inject events.
Then, implement the Subscribe method. When the “greetings” item is subscribed to by the first user, the Adapter receives that method call and starts a thread that will generate the real-time data. If more users subscribe to the “greetings” item, the Subscribe method is not called any more. When the last user unsubscribes from this item, the Adapter is notified through the Unsubscribe call. In this case we stop the publisher thread for that item. If a new user re-subscribes to “greetings”, the Subscribe method is called again. As already mentioned in the previous installment, this approach avoids consuming processing power for items nobody is currently interested in.
The Run method is executed within the thread started by Subscribe. Its code is very simple. We create a Hashtable containing a message (alternating “Hello” and “World”) and the current timestamp. Then we inject the Hashtable into the server through the listener. We wait for a random time between 1 and 3 seconds, and we are ready to generate a new event.
Save and build
From the File menu, choose “Save All“. Type a location, for example: “c:\”.
From the Build menu, choose “Build Solution“. Your C# Data Adapter is now compiled. If you used the path above, you will find your executable file under “C:\adapter_csharp\adapter_csharp\bin\Release”. It is called “adapter_csharp.exe“. But be patient and don’t start it now, because you have to configure and start the Lightstreamer Server first. You can skip the section below, dedicated to VB, and go straight to the “Deploy the Proxy Adapter” section.
Creating the Visual Basic Data Adapter
Now let’s work with Visual Basic instead of C#.
We will create a module (DataAdapterLauncher) which contains the application’s Main and initializes the DataProviderServer (the provided piece of code that implements the Network Protocol). Then we will create a class (HelloWorldAdapter) that implements the actual Adapter interface.
- Create a new VB project (I used Microsoft’s Visual Basic 2008 Express Edition).
- From the “New Project…” wizard, choose the “Console Application” template. Let’s use “adapter_vb” as the project name.
- From the “Solution Explorer”, delete the default Module1.vb, then add a reference to the Lightstreamer .NET library: go to the “Browse” tab of the “Add Reference” dialog and point to the DotNetAdapter_N2.dll file, which you can find in the “Lightstreamer\DOCS-SDKs\sdk_adapter_dotnet\lib\dotnet_2.0″ folder of your Lightstreamer installation.
DataAdapterLauncher
Add a new module (let’s call its file DataAdapterLauncher.vb). Replace the source code with this:
1 |
Imports System.Net.Sockets |
This code creates a DataProviderServer instance and assigns a HelloWorldAdapter instance (which we will define below) to it. Then, it creates two TCP client sockets, because the Proxy Data Adapter, to which our remote .NET Adapter will connect, needs two connections (but as I said, after creating these sockets, you don’t have to bother with reading and writing, as these operations are automatically handled by the DataProviderServer). Let’s use TCP ports 6661 and 6662. Assign the stream of the first socket to the RequestStream and ReplyStream properties of the DataProviderServer. Assign the stream of the second socket to the NotifyStream property of the DataProviderServer. Finally, start the DataProviderServer.
HelloWorldAdapter
Now add a class (call its file HelloWorld.vb). Replace the source code with this:
1 |
Imports System.Collections |
The HelloWorldAdapter class implements the IDataProvider interface (which is the .NET remote equivalent of the Java DataProvider interface).
Implement the SetListener subroutine to receive a reference to the server’s listener that you will use to inject events.
Then, implement the Subscribe subroutine. When the “greetings” item is subscribed to by the first user, the Adapter receives that subroutine call and starts a thread that will generate the real-time data. If more users subscribe to the “greetings” item, the Subscribe subroutine is not called any more. When the last user unsubscribes from this item, the Adapter is notified through the Unsubscribe call. In this case we stop the publisher thread for that item. If a new user re-subscribes to “greetings”, the Subscribe subroutine is called again.
The Run subroutine is executed within the thread started by Subscribe. Its code is very simple. We create a Hashtable containing a message (alternating “Hello” and “World”) and the current timestamp. Then we inject the Hashtable into the server through the listener. We wait for a random time between 1 and 3 seconds, and we are ready to generate a new event.
Save and build
From the File menu, choose “Save All“. Type a location, for example: “c:\”.
From the Build menu, choose “Build adapter_vb“. Your VB Data Adapter is now compiled. If you used the path above, you will find your executable file under “C:\adapter_vb\adapter_vb\bin\Release”. It is called “adapter_vb.exe“. As for the C# Adapter, don’t start it now, because you have to configure and start the Lightstreamer Server first.
Deploying the Proxy Adapter
Now that our remote Data Adapter is ready, we need to deploy and configure the provided Proxy Adapter within Lightstreamer Server.
Go to the “adapters” folder of your Lightstreamer Server and create a “ProxyHelloWorld” folder inside “adapters”, and a “lib” folder inside “ProxyHelloWorld”.
Copy the “ls-proxy-adapters.jar” file from “Lightstreamer/DOCS-SDKs/sdk_adapter_remoting_infrastructure/lib” to “Lightstreamer/adapters/ProxyHelloWorld/lib”.
Create a new file in “Lightstreamer/adapters/ProxyHelloWorld”, call it “adapters.xml“, and use the following contents:
1 |
<?xml version="1.0"?> |
You have just deployed a new Java Adapter pair, where the Metadata Adapter is a default one (called “LiteralBasedProvider”) and the Data Adapter is the Proxy Adapter (called “RobustNetworkedDataProvider”). This Adapter pair will be referenced by the clients as “PROXY_HELLOWORLD“.
As a final configuration, let’s tell our Web client to use this new Adapter pair, rather than those we developed last time. So just edit the “index.htm” page of the Hello World front-end (last time we deployed it under “Lightstreamer/pages/HelloWorld”) and replace:
engine.connection.setAdapterName("HELLOWORLD");
with:
engine.connection.setAdapterName("PROXY_HELLOWORLD");
Running the application
Now we have all the pieces ready. Let’s enjoy the results.
Start your Lightstreamer Server. When it’s up, run either the C# or the VB Remote Adapter.
Open a browser window and go to: http://localhost:8080/HelloWorld/
Final notes
Starting from these very simple code fragments we have developed in these two installments (covering basic JavaScript, Java, C#, and VB), I encourage you to experiment and create more complex applications. The full API references for all these languages are available from the links below:
- .NET API reference for Adapters
- Java API reference for Adapters
- JavaScript API reference for Clients
All the source code described in this article is available for download.