Template:PT3 JS ANY NODE: Difference between revisions

From Phidgets Support
(Created page with "<div class="phd-slide-deck" data-deck="PT3_JS_ANY_NODE"> <div class="phd-slide-deck-header"><h3>Language - JavaScript</h3></div> <div class="phd-slides"> <div class="phd-slide...")
 
No edit summary
 
Line 27: Line 27:
<br />
<br />


<h2>Version Change</h2>
<h2>Version History</h2>


<p>
<p><b>1.x.x</b> - Initial Release (unstable- highly recommend updating to 2.x.x or newer)</p>
<b>Note:</b> The Phidgets JavaScript library has been bumped to version 2.x.x following a rewrite. The version 2 API is mostly identical to version 1, but does
 
have some breaking changes. It is highly recommended that any code written against version 1 be updated to version 2, as version 1 is considered unstable.
<p><b>2.x.x</b> - Fixed stability issues</p>
</p>
 
<p><b>3.x.x</b> - Added WebUSB support for VINT devices</p>
 
<p>Each release has potential breaking changes, so you should always revisit your code when updating to a new major release.</p>
</div>
</div>


<div class="phd-slide">
<div class="phd-slide">
<h2>Phidget Network Server</h2>
<h2>JavaScript using the Phidget Network Server</h2>


<p>
<p>
The Phidgets JavaScript library requires the Phidget Network Server. Go to the page below and select the tab with your OS to get the Network Server set up:
Go to the <span>[https://www.phidgets.com/docs/Phidget_Network_Server#Using_The_Network_Server this page]</span> and select the tab with your OS to get the
Network Server set up.
</p>
</p>
<p>● <span>[https://www.phidgets.com/docs/Phidget_Network_Server#Using_The_Network_Server Phidget Network Server]</span></p>
<br />


<p>
<p>
Line 53: Line 53:
<p>
<p>
The Phidget Server Webserver can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript
The Phidget Server Webserver can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript
control panel files. The main purpose of the Webserver is to support a Websockets connection for the Browser library - because regular sockets cannot be used in
control panel files.
Browser. The Node.js library uses raw sockets to connect to the Phidget Server, and so does not require the Webserver or Websockets.
</p>
 
<p>
In order to connect remotely, you need to use the <b>NetworkConnection</b> object. When you download a code sample later in this guide, make sure the
<b>Remote</b> box is checked.
</p>
 
<h2>JavaScript using WebUSB</h2>
 
<p>
For library version 3.x.x or newer, you can connect to Phidgets plugged in locally via USB without having the Network Server running. This feature is only
supported on VINT Phidgets.
</p>
 
<p>
In order to connect using USB, you need to use the <b>USBConnection</b> object. When you download a code sample later in this guide, make sure the
<b>Remote</b> box is unchecked.
</p>
</p>
</div>
</div>
Line 102: Line 118:
<p>Once you've made your selections, click the <b>Download Example</b> button to download a sample script.</p>
<p>Once you've made your selections, click the <b>Download Example</b> button to download a sample script.</p>


<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_JS_ANY_NODE/-x320/50/JavaScript_Sample_Code_Download.png"></div>
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_JS_ANY_NODE/-x310/50/JavaScript_Sample_Code_Download.png"></div>
</div>
</div>



Latest revision as of 21:43, 22 April 2022

JavaScript in Node.js

Welcome to using Phidgets with JavaScript! By using JavaScript, you will have access to the complete Phidget22 API, including events.

Node.js is an open-source, cross-platform JavaScript run-time environment that allows programs written in JavaScript to be run locally.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers on the computer that will be running the server (see Part 1 of this user guide)

Node.js


Version History

1.x.x - Initial Release (unstable- highly recommend updating to 2.x.x or newer)

2.x.x - Fixed stability issues

3.x.x - Added WebUSB support for VINT devices

Each release has potential breaking changes, so you should always revisit your code when updating to a new major release.

JavaScript using the Phidget Network Server

Go to the this page and select the tab with your OS to get the Network Server set up.

The Phidget Server includes a built-in Webserver. This must be enabled when using the JavaScript library in browser, but can be left disabled when using the library from Node.js.

The Phidget Server Webserver can be used to serve files - such as the Phidget JavaScript library, or your own projects. By default, it serves the JavaScript control panel files.

In order to connect remotely, you need to use the NetworkConnection object. When you download a code sample later in this guide, make sure the Remote box is checked.

JavaScript using WebUSB

For library version 3.x.x or newer, you can connect to Phidgets plugged in locally via USB without having the Network Server running. This feature is only supported on VINT Phidgets.

In order to connect using USB, you need to use the USBConnection object. When you download a code sample later in this guide, make sure the Remote box is unchecked.

JavaScript Control Panel

The JavaScript control panel is a Browser version of our Phidget control panel. This can be used to view and control all Phidgets attached to a Phidget server. The JavaScript control panel is installed by default on Windows, macOS and PhidgetSBC. You can also download the source here.

Make sure the Phidget Server - Webserver is enabled, and running, then navigate to http://localhost:8989. (If you changed the port setting on the Webserver, replace '8989' with your selected port)

JavaScript Control Panel

You will now see a program written with JavaScript/HTML that mimics the Phidget Control Panel. It will show all the Phidgets attached to your machine. By double-clicking on the Phidgets, and example will launch.

Using the Code Samples

Now that you've confirmed the webserver is running properly by testing your Phidgets through the JavaScript Control Panel, you can try running some of our sample code. On the Code Samples page and select your device from the drop-down menu.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the Download Example button to download a sample script.

Using the Code Samples

Next, unpack the example and open the command prompt in the folder you extracted to and enter the following commands:

npm install phidget22
npm update

Then enter the following command to run the example (replacing example.js with your example name):

node example.js

Success! Your program is now running with Phidgets.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»