This repository demonstrates how to access Android devices using Node.js, and forward the ADB connection to Web clients.
- List Android devices connected to the server over USB
- Mirror the device screen and control the device from a Web client
- The server uses Tango in Direct Connection mode to connect to Android devices over USB.
- It creates a custom protocol to forward ADB sockets to Web clients over WebSocket.
- The Web client creates a custom transport that converts WebSocket messages back to ADB sockets.
- The Web client creates an
Adb
instance to operate on the device.
The server can share a device with multiple clients, because the client operates on ADB socket level.
Comlink library is used to simplify the communication between the server and the client. It allows calling functions on the server from the client as if they were local functions.
Comlink doesn't have built-in support for WebSocket connections, so a custom endpoint object is implemented to serialize and deserialize Comlink messages using MsgPack format.
Note that although Comlink is very easy to use, it's definitely not the most efficient way, due to its messaging overhead. Sending raw data on the WebSocket connection will be more efficient, for example using one WebSocket connection for each ADB socket.
git clone --recurse-submodules https://github.com/tango-adb/demo-nodejs.git
pnpm recursive i
pnpm recursive run build
cd server
pnpm start
The server listens on port 8081.
cd web
pnpm start
The client project has its own server listening on port 3000.
In this demo, the client uses Tango to serialize and deserialize ADB commands, and the server uses Tango to send other commands to the device.
It can be safer to only use Tango on the server side, and creating API endpoints for each feature you want to expose to the client. This way, the client can't execute arbitrary ADB commands, but only the ones you expose.