LabVIEW WebSockets API / Library
Links / Examples
This library allows a web browser to communicate with a LabVIEW application acting as a WebSocket server. Web Sockets offers low-latency communications between a LabVIEW application and a web browser with no plug-ins or LabVIEW run-time engine installation requirements.
It contains VIs for performing Web Sockets handshaking, reading/writing data and closing the connection. The API allows code to be written for your LabVIEW application to act as both a Web Sockets server (e.g. communicate with a browser), and as a client (e.g. to talk to existing WebSockets services).
Potential uses for this library include:
- Remote Monitoring: Enabling LabVIEW applications with WebSockets allows communication with an application running remotely over a LAN (or WAN/VPN) network. This would allow monitoring/control of applications running in a remote environment (e.g. in a lab/workshop/production area) from the office.
- LabVIEW RT / Embedded User Interfaces: Using WebSockets on an RT Target (such as an sbRIO / CompactRIO), stylish user interfaces can be created for embedded targets or products.
- Mobile Interfaces: Most modern smartphones support the WebSockets protocol, allowing bespoke user interfaces for monitoring/control of LabVIEW applications from a mobile device such as a phone or tablet.
- Multi-User Interfaces: The WebSockets server can support multiple simultaneous clients, allowing multiple users to interface with the application at the same time.
As it uses the native TCP/IP VIs in LabVIEW, it works on both desktop LabVIEW (e.g. Windows, Linux etc.) and also LabVIEW RT (e.g. CompactRIO, sbRIO etc.).
MediaMongrels Ltd has extensive experience of using this library to create dynamic web-based user interfaces for LabVIEW applications. If you would like to use our services to incorporate the library into your application, please contact us to discuss your requirements.
Download / Installation
The library is published as a VI Package on the NI Tools Network - it can be found in the NI Tools Network repository in VIPM or installed by clicking the link below.
The VI Package is supported in LabVIEW 2013 or later.
- V18.104.22.168 (19/10/2016)
The package has now been released on the NI LabVIEW Tools Network package repository. This and future versions will be available directly through VIPM (vipm://mediamongrels_ltd_lib_websockets_api).
Changes in this version include:
- Fixed an issue with the incorrect EOL being used in InitiateClientHandshake.vi for better cross-platform support.
- Added support for sending additional headers in the client handshake / server response
- Added support for Sec-WebSocket-Protocol: The DoHandshake.vi now has a 'supported protocols' input which allows supported protocols to be listed (in order of preference). This will increase compatibility with existing WebSocket servers.
- The package is now released under the MIT License
Note: The VI package has also changed name. Please uninstall any previous versions of this package before upgrading.
- V22.214.171.124 (20/04/2015)
- Added functionality to be able to act as a WebSockets client - incoming/outgoing messages are now de-masked correctly and optionally masked for transmission. Added VI for creating & sending the HTTP handshake. This WS client functionality has not been fully tested (e.g. with external WS servers) but has been tested with a LabVIEW server using my library.
- V126.96.36.199 (04/02/2015)
- Made mask values more clear in source and updated calculation of message length bytes. (Thanks Stuart)
- V188.8.131.52 (14/01/2015)
- Fixed an error in accepting the request due to casing of the initial HTTP headers sent by IE.
- V184.108.40.206 (19/12/2014)
- Fixed an error in DoHandshake that was causing the VI to finish only on timeout instead of at the end of the HTTP header (causing connections to take longer than required).
Usage / Licensing
The package/source is released under the MIT License. You will be asked to read/accept the license when installing the VI Package.
LabVIEW users familiar with the TCP/IP VIs should have no problems integrating the WebSockets API into their applications. The library is a wrapper for the native functions and usage is similar. A simple example of how to use the library is shown below, along with a description of each VI.
Do Handshake: Performs the WebSocket handshaking after a connection is made - reads the headers from the HTTP request and replies with the appropriate WebSocket key to open the connection.
Do Client Handshake: This VI allows you to initiate a connection acting as a WebSocket client, communicating to an existing WebSocket server (either implementing in LabVIEW or otherwise). It sends the necessary HTTP header to start a WebSocket connection and waits for the reply from the server.
Get Header: I store the header name/value pairs as variant attributes, you can use this VI to retrieve a header value.
Read: Wrapper for the TCP Read primitive that splits out the data from the WebSocket frame. The returned Opcode is as per the WebSocket standard.
Write: Wrapper for the TCP Write primitive that packages the data into a WebSocket frame and sends to the connection.
Close: Sends the WebSocket close Opcode to the browser. According to the WebSockets spec, both sides of the connection are supposed to send the close Opcode before terminating the TCP connection so this VI doesn't close the TCP connection.