|
| 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 | +``` |
0 commit comments