Example Code

Web Service Based Chat Program using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Overview
This is an example of a simple chat room, implemented with LabVIEW's Web Services.  This demonstrates how to use LabVIEW's Web Services to create a distributed application.


Description

  • Overal Architecture
  • Client Application
  • Client HTTP Commands
  • Server Application
  • Web Methods and Responses
  • Installing and running

 

Overall Architecture

In most chat programs, there is a central server, where all messages are routed.  There is also a client, which runs on each participant's machine.  Several kinds of information are passed back and forth, including login information, logout info, lists of people logged on, and chat messages.  In this application, there is a separate web method for each.  When the different parts of the client application wish to contact a given part of the server, it makes it fairly simple to contact the correct part of the server without any interference from other parts.  That is one of the advantages of Web Services. A schematic for this design looks like this:

 

web service protocol overview.png

 

In addition to the web methods, the server uses a functional global variable to keep track of partcipants currently logged on.  This streamlined protocol stands in contrast to similar applications developed with UDP or TCP.  In that case, only one part of the process can receive information.  To get around that, one approach is to use multiple ports.  Another is to use one process to receive the incoming data and parse it, sending it to other parts of the application that need it.  One such architecture schematic might look like this (neglecting interprocess communication):

confusion of tcp.png

 

This difference in complexity makes Web Services a great choice for many applications.

 

Client Application

The client application is a VI (or exe) that runs on each user's computer.  This application is a UI event-based application, that sends in chats and pulls updates down from the server, in addition to other details.  Here is the "Send" case, where chats are sent to the server, the Textbox of recent chats is retrieved, the list of logged in participants is retrieved, and the boxes are cleared and repositioned:

 

client VI.png

 

Client HTTP Commands

In those subVIs, the HTTP VIs are used when communicating with the server.  These VIs are quite easy to use.  Here, the chats are sent in and a recent chat list is retrieved.  The data is assembled into the proper format, and sent into the server with a POST call.  The recent chat data is returned as simple as that:

 

 

client send chat.png

 

Server Application

The server has several web methods and a functional global to manage the participants list. The Chats method now deserves a little attention.  The chats are received by this method, and after some data scrubbing, are assembled with the sender's name and stored into an ongoing array of all past chats (since the last server reset, of course).  Also submitted is a user-specified number of chats to retrieve.  That number of chats are assembled into a single string and returned to the sender.

server chat service.png

 

Web Methods and Responses

The other web methods are all similar to this one, the method for logging on.  Data is retrieved with the Read Postdata VI, and processed with a functional global variable.  Then return data is sent:

 

server logon.png

 

In order to return data to the user, we can use either over the terminals or using MIME data.  The terminals offers reduced functinality, but are simpler to use.  This application uses the latter method.

 

The kind of data is set in the first VI, then the data is inserted with the next.  Finally, the buffer is flushed at the end of the transaction to push the info back to the sender.

 

Installing and Running

Installing the project is as easy as downloading the .zip.  But before the application will work, the server web service must be built and deployed.  The build specifications are already set, so building and deploying on the machine of your choice should be sufficient.  Once the server is deployed, the IP address should be given to the clients so they can connect.

 

In addition, there is a build spec for a client exe version, for cases where that is more appropriate.  Then you can distribute your code without giving away your newfound secrets and the power of LabVIEW Web Services.

 

Requirements

  • LabVIEW Base Development System 2012 (or compatible)
  • LabVIEW Application Builder


Steps to Implement or Execute Code

  1. Unzip the attached folder "Web-Based Chat 2012 NIVerified.zip"
  2. Open "Web-Based Chat 2012 NIVerified.lvproj"
  3. Right click "chat server" under Build Specifications and select Build
  4. After the build process completes, right click "chat server" and select Deploy
  5. After the chat server is deployed, open "Web Chat Client 2012 NIVerified.vi"
  6. Run the VI
  7. Enter the Screenname and IP address of the PC where the server is deployed, and click "OK"
  8. Type message and click "Send"
  9. Click "Stop" to finish conversation

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors