To download NI software, including the products shown below, visit ni.com/downloads.
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
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:
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):
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 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:
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.
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:
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
Steps to Implement or Execute Code
**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.