Skip to content

Commit 13b5f98

Browse files
authored
Updated readme with example use-case (#86)
* updated readme example with example use-case * added notes re. streaming multiple applications * added streaming examples, fix for multiple streams * added changeset
1 parent 64f142a commit 13b5f98

8 files changed

Lines changed: 286 additions & 28 deletions

File tree

.changeset/fruity-zoos-try.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@e2b/desktop-python': patch
3+
'@e2b/desktop': patch
4+
---
5+
6+
fix overwriting .vnc directory when stream is re-started

README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ Each sandbox is isolated from the others and can be customized with any dependen
88

99
## Examples
1010

11-
**Basic SDK Examples**
11+
**SDK Examples**
1212

13-
- Check out the examples directory for more examples on how to use the SDK:
13+
- Basic Examples:
1414
- [Python](./examples/basic-python)
1515
- [JavaScript](./examples/basic-javascript)
16+
- Streaming Desktop Applications:
17+
- [Python](./examples/streaming-apps-python)
18+
- [JavaScript](./examples/streaming-apps-javascript)
1619

1720
**[Open Computer Use](https://github.com/e2b-dev/open-computer-use)**
1821

@@ -52,31 +55,63 @@ npm install @e2b/desktop
5255
```python
5356
from e2b_desktop import Sandbox
5457

55-
# Basic initialization
58+
# Create a new desktop sandbox
5659
desktop = Sandbox()
5760

58-
# With custom configuration
59-
desktop = Sandbox(
60-
display=":0", # Custom display (defaults to :0)
61-
resolution=(1920, 1080), # Custom resolution
62-
dpi=96, # Custom DPI
61+
# Launch an application
62+
desktop.launch('google-chrome') # or vscode, firefox, etc.
63+
64+
# Wait 10s for the application to open
65+
desktop.wait(10000)
66+
67+
# Stream the application's window
68+
# Note: There can be only one stream at a time
69+
# You need to stop the current stream before streaming another application
70+
desktop.stream.start(
71+
window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed
72+
require_auth=True
6373
)
74+
75+
# Get the stream auth key
76+
auth_key = desktop.stream.get_auth_key()
77+
78+
# Print the stream URL
79+
print('Stream URL:', desktop.stream.get_url(auth_key=auth_key))
80+
81+
# Kill the sandbox after the tasks are finished
82+
# desktop.kill()
6483
```
6584

6685
**JavaScript**
6786

6887
```javascript
6988
import { Sandbox } from '@e2b/desktop'
7089

71-
// Basic initialization
90+
// Start a new desktop sandbox
7291
const desktop = await Sandbox.create()
7392

74-
// With custom configuration
75-
const desktop = await Sandbox.create({
76-
display: ':0', // Custom display (defaults to :0)
77-
resolution: [1920, 1080], // Custom resolution
78-
dpi: 96, // Custom DPI
93+
// Launch an application
94+
await desktop.launch('google-chrome') // or vscode, firefox, etc.
95+
96+
// Wait 10s for the application to open
97+
await desktop.wait(10000)
98+
99+
// Stream the application's window
100+
// Note: There can be only one stream at a time
101+
// You need to stop the current stream before streaming another application
102+
await desktop.stream.start({
103+
windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed
104+
requireAuth: true,
79105
})
106+
107+
// Get the stream auth key
108+
const authKey = desktop.stream.getAuthKey()
109+
110+
// Print the stream URL
111+
console.log('Stream URL:', desktop.stream.getUrl({ authKey }))
112+
113+
// Kill the sandbox after the tasks are finished
114+
// await desktop.kill()
80115
```
81116

82117
## Features
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Streaming Applications Example
2+
3+
Streaming applications is a feature of the E2B Desktop Sandbox.
4+
5+
> [!WARNING]
6+
>
7+
> - Will raise an error if the desired application is not open yet
8+
> - The stream will close once the application closes
9+
> - Creating multiple streams at the same time is not supported, you may have to stop the current stream and start a new one for each application
10+
11+
## How to run
12+
13+
### 1. Get E2B API key
14+
15+
Sign up at [E2B](https://e2b.dev) and get your API key.
16+
Set environment variable `E2B_API_KEY` with your API key.
17+
18+
### 2. Install SDK
19+
20+
```bash
21+
npm install @e2b/desktop
22+
```
23+
24+
### 3. Create Desktop Sandbox
25+
26+
```javascript
27+
import { Sandbox } from '@e2b/desktop'
28+
29+
// Start a new desktop sandbox
30+
const desktop = await Sandbox.create()
31+
console.log('Desktop sandbox created', desktop.sandboxId)
32+
33+
// Launch an application
34+
console.log('Launching Google Chrome')
35+
await desktop.launch('google-chrome') // or vscode, firefox, etc.
36+
37+
// Wait 10s for the application to open
38+
await desktop.wait(10000)
39+
40+
// Stream the application's window
41+
// Note: there can be only one stream at a time
42+
// You need to stop the current stream before streaming another application
43+
console.log('Starting to stream Google Chrome')
44+
await desktop.stream.start({
45+
windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed
46+
requireAuth: true,
47+
})
48+
49+
// Get the stream auth key
50+
const authKey = desktop.stream.getAuthKey()
51+
52+
// Print the stream URL
53+
console.log('Stream URL:', desktop.stream.getUrl({ authKey }))
54+
55+
// Do some actions in the application
56+
console.log('Writing to Google Chrome')
57+
await desktop.write('What is the capital of Germany?')
58+
59+
console.log('Pressing Enter')
60+
await desktop.press('Enter')
61+
62+
// wait 15s for page to load
63+
console.log('Waiting 15s')
64+
await desktop.wait(15000)
65+
66+
// Stop the stream
67+
console.log('Stopping the stream')
68+
await desktop.stream.stop()
69+
70+
// Open another application
71+
console.log('Launching VS Code')
72+
await desktop.launch('code')
73+
74+
// Wait 10s for the application to open
75+
await desktop.wait(10000)
76+
77+
// Start streaming the new application
78+
console.log('Starting to stream VS Code')
79+
await desktop.stream.start({
80+
windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed
81+
requireAuth: true,
82+
})
83+
84+
// Get the stream auth key
85+
const authKey2 = desktop.stream.getAuthKey()
86+
87+
// Print the stream URL
88+
console.log('Stream URL:', desktop.stream.getUrl({ authKey: authKey2 }))
89+
90+
// Kill the sandbox after the tasks are finished
91+
// await desktop.kill()
92+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Streaming Applications Example
2+
3+
Streaming applications is a feature of the E2B Desktop Sandbox.
4+
5+
> [!WARNING]
6+
>
7+
> - Will raise an error if the desired application is not open yet
8+
> - The stream will close once the application closes
9+
> - Creating multiple streams at the same time is not supported, you may have to stop the current stream and start a new one for each application
10+
11+
## How to run
12+
13+
### 1. Get E2B API key
14+
15+
Sign up at [E2B](https://e2b.dev) and get your API key.
16+
Set environment variable `E2B_API_KEY` with your API key.
17+
18+
### 2. Install SDK
19+
20+
```bash
21+
pip install e2b-desktop
22+
```
23+
24+
### 3. Create Desktop Sandbox
25+
26+
```python
27+
from e2b_desktop import Sandbox
28+
29+
# Create a new desktop sandbox
30+
desktop = Sandbox()
31+
print('Desktop sandbox created', desktop.sandbox_id)
32+
33+
# Launch an application
34+
print('Launching Google Chrome')
35+
desktop.launch('google-chrome') # or vscode, firefox, etc.
36+
37+
# Wait 10s for the application to open
38+
desktop.wait(10000)
39+
40+
# Stream the application's window
41+
# Note: there can be only one stream at a time
42+
# You need to stop the current stream before streaming another application
43+
print('Starting to stream Google Chrome')
44+
desktop.stream.start(
45+
window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed
46+
require_auth=True,
47+
)
48+
49+
# Get the stream auth key
50+
auth_key = desktop.stream.get_auth_key()
51+
52+
# Print the stream URL
53+
print('Stream URL:', desktop.stream.get_url(auth_key=auth_key))
54+
55+
# Do some actions in the application
56+
print('Writing to Google Chrome')
57+
desktop.write('What is the capital of Germany?')
58+
59+
print('Pressing Enter')
60+
desktop.press('enter')
61+
62+
# wait 15s for page to load
63+
print('Waiting 15s')
64+
desktop.wait(15000)
65+
66+
# Stop the stream
67+
print('Stopping the stream')
68+
desktop.stream.stop()
69+
70+
# Open another application
71+
print('Launching VS Code')
72+
desktop.launch('code')
73+
74+
# Wait 10s for the application to open
75+
desktop.wait(10000)
76+
77+
# Start streaming the new application
78+
print('Starting to stream VS Code')
79+
desktop.stream.start(
80+
window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed
81+
require_auth=True,
82+
)
83+
84+
# Get the stream auth key
85+
auth_key2 = desktop.stream.get_auth_key()
86+
87+
# Print the stream URL
88+
print('Stream URL:', desktop.stream.get_url(auth_key=auth_key2))
89+
90+
# Kill the sandbox after the tasks are finished
91+
# desktop.kill()
92+
```

packages/js-sdk/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,31 @@ npm install @e2b/desktop
3939
```javascript
4040
import { Sandbox } from '@e2b/desktop'
4141

42-
// Basic initialization
42+
// Start a new desktop sandbox
4343
const desktop = await Sandbox.create()
4444

45-
// With custom configuration
46-
const desktop = await Sandbox.create({
47-
display: ':0', // Custom display (defaults to :0)
48-
resolution: [1920, 1080], // Custom resolution
49-
dpi: 96, // Custom DPI
45+
// Launch an application
46+
await desktop.launch('google-chrome') // or vscode, firefox, etc.
47+
48+
// Wait 10s for the application to open
49+
await desktop.wait(10000)
50+
51+
// Stream the application's window
52+
// Note: there can be only one stream at a time
53+
// You need to stop the current stream before streaming another application
54+
await desktop.stream.start({
55+
windowId: await desktop.getCurrentWindowId(), // if not provided the whole desktop will be streamed
56+
requireAuth: true,
5057
})
58+
59+
// Get the stream auth key
60+
const authKey = desktop.stream.getAuthKey()
61+
62+
// Print the stream URL
63+
console.log('Stream URL:', desktop.stream.getUrl({ authKey }))
64+
65+
// Kill the sandbox after the tasks are finished
66+
// await desktop.kill()
5167
```
5268

5369
## Features

packages/js-sdk/src/sandbox.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ class VNCServer {
643643
private async getVNCCommand(windowId?: string): Promise<string> {
644644
let pwdFlag = '-nopw'
645645
if (this.novncAuthEnabled) {
646-
await this.desktop.commands.run('mkdir ~/.vnc')
646+
// Create .vnc directory if it doesn't exist
647+
await this.desktop.commands.run('mkdir -p ~/.vnc')
647648
await this.desktop.commands.run(
648649
`x11vnc -storepasswd ${this.password} ~/.vnc/passwd`
649650
)

packages/python-sdk/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,31 @@ pip install e2b-desktop
3939
```python
4040
from e2b_desktop import Sandbox
4141

42-
# Basic initialization
42+
# Create a new desktop sandbox
4343
desktop = Sandbox()
4444

45-
# With custom configuration
46-
desktop = Sandbox(
47-
display=":0", # Custom display (defaults to :0)
48-
resolution=(1920, 1080), # Custom resolution
49-
dpi=96, # Custom DPI
45+
# Launch an application
46+
desktop.launch('google-chrome') # or vscode, firefox, etc.
47+
48+
# Wait 10s for the application to open
49+
desktop.wait(10000)
50+
51+
# Stream the application's window
52+
# Note: There can be only one stream at a time
53+
# You need to stop the current stream before streaming another application
54+
desktop.stream.start(
55+
window_id=desktop.get_current_window_id(), # if not provided the whole desktop will be streamed
56+
require_auth=True
5057
)
58+
59+
# Get the stream auth key
60+
auth_key = desktop.stream.get_auth_key()
61+
62+
# Print the stream URL
63+
print('Stream URL:', desktop.stream.get_url(auth_key=auth_key))
64+
65+
# Kill the sandbox after the tasks are finished
66+
# desktop.kill()
5167
```
5268

5369
## Features

packages/python-sdk/e2b_desktop/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def start(self, vnc_port: Optional[int] = None, port: Optional[int] = None, requ
144144
# Set up VNC command
145145
pwd_flag = "-nopw"
146146
if self._novnc_auth_enabled:
147-
self.__desktop.commands.run("mkdir ~/.vnc")
147+
self.__desktop.commands.run("mkdir -p ~/.vnc")
148148
self.__desktop.commands.run(f"x11vnc -storepasswd {self._novnc_password} ~/.vnc/passwd")
149149
pwd_flag = "-usepw"
150150

0 commit comments

Comments
 (0)