Skip to content

TCP bridges

A TCP bridge lets a page of the app talk to a TCP service that only the app’s server can reach, such as a server’s VNC or SSH port, or a database, on the workspace’s private network. The browser opens an ordinary WebSocket to the app, and the server connects to the service and passes the bytes through both ways. The user’s device joins no network and installs no VPN client.

A bridge only carries bytes. A console, a terminal or a database tool that speaks the service’s protocol is a page or plugin of your own built on it.

A bridge is an API function whose handler is tcpBridge({ target }). target(name, context) is the app’s access rule: given the name a page asks for, such as a server’s record key, it answers where the caller may connect, { host, port, expiresInSeconds? }, or undefined when they may not. context is the function’s context, with the caller’s session and records, so the rule can read a record as the caller. The function’s own roles or capability decide who may call it at all.

docs-examples/bridges/workshop-console.ts
import { defineFunction, tcpBridge } from '@protobase/server'
// The workshop's machines on the private network, by the name a page asks for.
const machines: Record<string, { host: string; port: number }> = {
'truing-stand': { host: '10.20.0.11', port: 5900 },
'stock-db': { host: '10.20.0.2', port: 5432 },
}
// /api/functions/workshop-console: mechanics reach the truing stand's screen for an hour at a time; only admins reach
// the stock database, and only for ten minutes.
export const workshopConsole = tcpBridge({
target: (name, { session }) => {
const machine = machines[name]
if (!machine) return
if (name !== 'stock-db') return machine
return session.user.roles.includes('admin') ? { ...machine, expiresInSeconds: 600 } : undefined
},
})
export default defineFunction({ roles: ['mechanic', 'admin'] }, workshopConsole)

host is any host name or address the server can reach. expiresInSeconds is how long the bridge stays open, from when its token was issued: a whole number of seconds up to a day, an hour by default.

  1. POST /api/functions/<name> with { "target": "<name>" } runs the rule. It answers 201 with { token, open_by, expires_at }, or a 404 problem for a target the caller may not reach, without connecting anywhere. A user has at most 20 tokens waiting to be used; asking for another drops their oldest.
  2. A WebSocket to /api/functions/<name>?token=<token>, before open_by, 30 seconds after the token was issued, opens the bridge. The token opens one bridge, for the same caller only; a token used before, too late, or by someone else is a 404. The rule runs again, then the server connects to the target, and a target that does not answer within 10 seconds is a 502 that names no address. A bridge whose expires_at passes, or whose target is withdrawn, while the rule runs or the server connects does not open either.
  3. Each binary message is written to the service as it is, and what the service sends comes back as binary messages, in order. A text message closes the bridge with 1003.

client.bridges.open(name, target) of @protobase/client does both steps and resolves to { socket, expiresAt }, with the socket’s binary as ArrayBuffers, as soon as the socket exists; wait for its open event before sending:

docs-examples/bridges/open-truing-stand.ts
import { createClient } from '@protobase/client'
const client = createClient()
// A page of the app opens the truing stand's bridge and hands its bytes to a viewer of its own, such as a VNC client.
// `open` resolves as soon as the socket exists, so this waits for it to open before handing out its sender.
export const openTruingStand = async (show: (bytes: Uint8Array) => void) => {
const { socket, expiresAt } = await client.bridges.open('workshop-console', 'truing-stand')
socket.onmessage = (event) => show(new Uint8Array(event.data as ArrayBuffer))
socket.onclose = (event) => console.log('closed', event.code, event.reason, 'expires at', expiresAt)
await new Promise<void>((resolve, reject) => {
socket.addEventListener('open', () => resolve(), { once: true })
socket.addEventListener('close', () => reject(new Error('The truing stand could not be reached')), { once: true })
})
return (bytes: ArrayBuffer) => socket.send(bytes)
}
Close code Why
1000 The service closed the connection.
1008 The bridge reached expires_at; open a new one with a new token. The app withdrew the target. The caller signed out or lost the function’s roles.
1011 The connection to the service failed.
1003 The page sent a text message.
  • Expiry. The server closes a bridge at its expires_at, whatever the rule answers since.
  • The user leaves. Closing the page closes the socket and the TCP connection with it. A client that vanishes without closing, such as a laptop gone to sleep, is closed once it misses a ping, within a minute. The caller is checked again every minute, as on every function socket.
  • The app withdraws the target. The rule runs again every minute while a bridge is open, and an undefined or a different host and port closes it. To close the bridges to a target at once, open or still opening, call withdraw(name) on the bridge, such as workshopConsole.withdraw('stock-db'), from code in the same process.

Data flows at the pace of the slower side, one chunk in flight each way. The server writes the next message to the service once the service’s connection has room again; until then later messages wait as on any function socket, which stops reading from the page above 1 MiB of them. It reads from the service again only once the page’s socket took the last chunk.