Skip to content

Commit 5c58da6

Browse files
authored
Add nodejs example (#377)
1 parent bdcf8a4 commit 5c58da6

File tree

9 files changed

+4478
-3
lines changed

9 files changed

+4478
-3
lines changed

examples/node/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
src/main.js
3+
.env.*

examples/node/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Viam SDK Quickstart - Node
2+
3+
This example demonstrates how to connect to a machine using Node.js.
4+
5+
## Usage
6+
7+
You must have a `.env` file in this directory with the following connection info which can be easily found in the TypeScript code sample for your machine.
8+
9+
```
10+
HOST="<HOST>"
11+
API_KEY_ID="<API_KEY_ID>"
12+
API_KEY="<API_KEY>"
13+
```
14+
15+
Installing will build the TypeScript SDK, then you can run the example using Vite.
16+
17+
```
18+
cd examples/node
19+
npm install
20+
npm start
21+
```
22+
23+
Edit `src/main.ts` to change the machine logic being run.
24+
25+
## Configuration
26+
27+
Using Viam's TypeScript SDK with Node.js requires some configurations. They are outlined below. Copying this project as a template is a good approach given the dependencies and polyfills requried. In the future, we hope to minimize the work neeed to get started here.
28+
29+
### Dependencies
30+
31+
The following direct dependencies are required:
32+
33+
- @connectrpc/connect-node
34+
- node-datachannel
35+
36+
In addition, polyfills and a node specific gRPC Transport are provided in `main.ts`.
37+
38+
#### `main.ts`
39+
40+
The `main.ts` file was updated to include the following polyfills and updates:
41+
42+
- WebRTC Polyfills:
43+
44+
```js
45+
import wrtc = require('node-datachannel/polyfill');
46+
for (const key in wrtc) {
47+
(global as any)[key] = (wrtc as any)[key];
48+
}
49+
```
50+
51+
- GRPC connection configuration
52+
```js
53+
import VIAM = require('@viamrobotics/sdk');
54+
globalThis.VIAM = {
55+
// @ts-ignore
56+
GRPC_TRANSPORT_FACTORY: (opts: any) =>
57+
connectNode.createGrpcTransport({ httpVersion: '2', ...opts }),
58+
};
59+
```

0 commit comments

Comments
 (0)