You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So far the networking examples focus on connecting to external networks or proxies (fetch proxy, WISP). I'm interested in local networking - communicating directly between JS and v86. This would open the door to use cases like running a server-side program (like Postgres, nginx, etc) inside v86 and communicate with it from JS.
I built tcpip.js to solve this. It's a virtual network stack that can run in the browser. It's built on top of lwIP compiled to WASM with bindings to hook into each layer of the network stack (L2, L3, L4).
Example use case
Set up plumbing that pipes ethernet packets between v86 and the virtual network stack.
import{createStack}from'tcpip';import{createV86NetworkStream}from'@tcpip/v86';conststack=awaitcreateStack();// Tap interfaces hook into ethernet (L2) framesconsttapInterface=awaitstack.createTapInterface({mac: '01:23:45:67:89:ab',ip: '192.168.1.1/24',});constemulator=newV86();// Adds net0 listener and exposes as readable/writable streamsconstvmNic=createV86NetworkStream(emulator);// Forward frames between the tap interface and the VM's NICtapInterface.readable.pipeTo(vmNic.writable);vmNic.readable.pipeTo(tapInterface.writable);
Now we can establish a TCP connection to the emulator from JS (assuming VM has IP 192.168.1.2 and a server listening on port 80):
constconnection=awaitstack.connectTcp({host: '192.168.1.2',port: 80,});// connection.writable is a standard WritableStreamconstwriter=connection.writable.getWriter();// Send dataawaitwriter.write(newTextEncoder().encode('Hello, world!'));awaitwriter.close();// Listen for incoming dataforawait(constchunkofconnection){console.log(newTextDecoder().decode(chunk));}
Or we can create a TCP server and listen for inbound connections:
constlistener=awaitstack.listenTcp({port: 80,});// TcpListener is an async iterable that yields TcpConnectionsforawait(constconnectionoflistener){constwriter=connection.writable.getWriter();// Send dataawaitwriter.write(newTextEncoder().encode('Hello, world!'));awaitwriter.close();// Listen for incoming dataforawait(constchunkofconnection){console.log(newTextDecoder().decode(chunk));}}
Other use cases
I'm planning to add bridge interface support soon, which would allow you to use tcpip.js as a switch/router. This can be useful for connecting more than 2 VMs together in a LAN.
I was wondering if you are interested in adding docs / examples for this use case? If so happy to put a PR together that adds some demos and networking docs.
The text was updated successfully, but these errors were encountered:
Hey 👋 amazing tool.
So far the networking examples focus on connecting to external networks or proxies (fetch proxy, WISP). I'm interested in local networking - communicating directly between JS and v86. This would open the door to use cases like running a server-side program (like Postgres, nginx, etc) inside v86 and communicate with it from JS.
I built tcpip.js to solve this. It's a virtual network stack that can run in the browser. It's built on top of lwIP compiled to WASM with bindings to hook into each layer of the network stack (L2, L3, L4).
Example use case
Set up plumbing that pipes ethernet packets between v86 and the virtual network stack.
Now we can establish a TCP connection to the emulator from JS (assuming VM has IP
192.168.1.2
and a server listening on port 80):Or we can create a TCP server and listen for inbound connections:
Other use cases
I'm planning to add bridge interface support soon, which would allow you to use tcpip.js as a switch/router. This can be useful for connecting more than 2 VMs together in a LAN.
I was wondering if you are interested in adding docs / examples for this use case? If so happy to put a PR together that adds some demos and networking docs.
The text was updated successfully, but these errors were encountered: