Skip to content

Commit 94fafe7

Browse files
committed
Add basic JavaScript example on how to use the Desktop SDK with streaming
1 parent bafc4be commit 94fafe7

File tree

6 files changed

+1050
-2
lines changed

6 files changed

+1050
-2
lines changed

examples/basic-js/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
E2B_API_KEY=your_api_key

examples/basic-js/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Desktop Sandbox JavaScript Example
2+
3+
This is a basic example of how to use the Desktop Sandbox JavaScript SDK with streaming and moving mouse around inside an Electron app.
4+
5+
## How to run
6+
7+
### 1. Create `.env` file
8+
9+
```bash
10+
cp .env.example .env
11+
```
12+
13+
### 2. Set `E2B_API_KEY` in `.env` file
14+
15+
Get your API key at [e2b.dev/dashboard](https://e2b.dev/dashboard).
16+
17+
```bash
18+
E2B_API_KEY="your_api_key"
19+
```
20+
21+
### 3. Install dependencies
22+
23+
```bash
24+
npm install
25+
```
26+
27+
### 4. Run
28+
29+
```bash
30+
npm start
31+
```

examples/basic-js/index.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'dotenv/config'
2+
import { app, BrowserWindow } from 'electron'
3+
import { Sandbox } from '@e2b/desktop'
4+
5+
const windowFrameHeight = 29
6+
7+
app.on('window-all-closed', () => {
8+
if (process.platform !== 'darwin') {
9+
app.quit()
10+
}
11+
})
12+
13+
app.on('activate', () => {
14+
if (BrowserWindow.getAllWindows().length === 0) {
15+
console.log('activate event received')
16+
createWindow()
17+
}
18+
})
19+
20+
function wait(ms) {
21+
return new Promise(resolve => setTimeout(resolve, ms))
22+
}
23+
24+
async function createWindow(streamUrl, width, height) {
25+
const win = new BrowserWindow({
26+
title: 'E2B Desktop',
27+
width,
28+
height: height + windowFrameHeight,
29+
webPreferences: {
30+
nodeIntegration: false,
31+
contextIsolation: true,
32+
webviewTag: true,
33+
},
34+
})
35+
36+
console.log('> Loading stream URL...')
37+
await win.loadURL(streamUrl)
38+
console.log(' - Stream URL loaded')
39+
}
40+
41+
async function moveAround(desktop, width, height) {
42+
console.log('\n> Randomly moving mouse and right clicking 5 times...')
43+
for (let i = 0; i < 5; i++) {
44+
const x = Math.floor(Math.random() * width)
45+
const y = Math.floor(Math.random() * height)
46+
await desktop.moveMouse(x, y)
47+
console.log(` - Moved mouse to ${x}, ${y}`)
48+
await desktop.rightClick()
49+
console.log(` - Right clicked ${i}`)
50+
console.log(' - Waiting 2 seconds...\n')
51+
await wait(2000)
52+
}
53+
}
54+
55+
async function main() {
56+
console.log('> Waiting for electron app to be ready...')
57+
await app.whenReady()
58+
console.log(' - Electron app is ready')
59+
60+
console.log('\n> Starting desktop sandbox...')
61+
const desktop = await Sandbox.create()
62+
console.log(' - Desktop sandbox started, ID:', desktop.sandboxId)
63+
64+
const size = await desktop.getScreenSize()
65+
console.log(' - Desktop sandbox screen size:', size)
66+
67+
console.log('\n> Starting desktop stream...')
68+
await desktop.stream.start()
69+
70+
console.log('\n> Waiting 5 seconds for the stream to load...')
71+
for (let i = 5; i > 0; i--) {
72+
console.log(` - ${i} seconds remaining until the next step...`)
73+
await wait(1000)
74+
}
75+
76+
const url = desktop.stream.getUrl()
77+
console.log(' - Stream URL:', url)
78+
79+
console.log('\n> Creating browser window...')
80+
createWindow(url, size.width, size.height)
81+
console.log(' - Browser window created')
82+
83+
await moveAround(desktop, size.width, size.height)
84+
85+
console.log('\nPress enter to kill the sandbox and close the window...')
86+
process.stdin.once('data', async () => {
87+
console.log('\n> Stopping desktop stream...')
88+
await desktop.stream.stop()
89+
console.log(' - Desktop stream stopped')
90+
91+
console.log('\n> Closing browser window...')
92+
console.log(' - Browser window closed')
93+
94+
console.log('\n> Killing desktop sandbox...')
95+
await desktop.kill()
96+
console.log(' - Desktop sandbox killed')
97+
98+
app.quit()
99+
})
100+
}
101+
102+
main().then().catch(console.error)

0 commit comments

Comments
 (0)