Skip to content

Port API ​

Provides PortHub-backed inter-window communication over named channels and point-to-point messaging.

port ​

port.connect ​

Open a port on a named channel for the calling window and announce it with port:connected. Any number of ports, from one window or several, can share a channel. A port belongs to the page that opened it and stays open until port.disconnect, until that page starts a top-level navigation (a reload or another address; same-document navigation such as a hash change does not count, and a navigation that ends in a download still does), or until its WebView goes away: the popup closes, the panel is removed, or the WebView is rebuilt or crashes beyond recovery. Each of these closes the port with port:disconnected before the next page can open ports.

ParameterTypeRequiredDescription
namestringYesChannel name; ports with the same name exchange messages. Must not be empty.

Returns

FieldTypeDescription
portIdstringId of the new port; the other port methods take it.
namestringThe channel name.
windowIdstringId of the calling window, which the port belongs to.

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
const res = await fb2k.invoke('port.connect', { name: 'lyrics' });
if (res.success === false) throw new Error(res.error);
const { portId } = res;

port.disconnect ​

Close a port and announce it with port:disconnected. Only the window that opened the port may close it; another window fails with PERMISSION_DENIED, and an id no open port has with PORT_NOT_FOUND.

ParameterTypeRequiredDescription
portIdstringYesId of the port to close. Must not be empty.

Returns

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
await fb2k.invoke('port.disconnect', { portId: 'port_00000001' });

port.getPorts ​

List the open ports, of every channel or of one.

ParameterTypeRequiredDescription
namestringNoList only the ports of this channel.

Returns

FieldTypeDescription
portsPortInfo[]The ports, in no particular order.
ports[].portIdstringId of the port; the other port methods take it.
ports[].windowIdstringId of the window the port belongs to, such as main.
ports[].namestringThe channel name.

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
const res = await fb2k.invoke('port.getPorts', { name: 'lyrics' });
if (res.success === false) throw new Error(res.error);
const { ports } = res;

port.postMessage ​

Send a message to every other port on the sender's channel, as a port:message event to each port's window. The sending port must belong to the calling window, otherwise the call fails with PERMISSION_DENIED; an unknown sending port fails with PORT_NOT_FOUND.

ParameterTypeRequiredDescription
portIdstringYesId of the sending port. Must not be empty.
messageanyYesThe message, delivered as message of the event. null counts as missing.

Returns

FieldTypeDescription
recipientsintegerHow many of the other ports' windows took the event.

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
await fb2k.invoke('port.postMessage', { portId: 'port_00000001', message: { text: 'hello' } });

port.postMessageTo ​

Send a message to one port, as a port:message event to that port's window. An unknown sending port fails with PORT_NOT_FOUND, a sending port of another window with PERMISSION_DENIED, an unknown target port with TARGET_NOT_FOUND, and a target window that did not take the event with OPERATION_FAILED.

ParameterTypeRequiredDescription
portIdstringYesId of the sending port. Must not be empty.
targetPortIdstringYesId of the port to deliver to. Must not be empty.
messageanyYesThe message, delivered as message of the event. null counts as missing.

Returns

success is true on success. On failure the response is { success: false, error, code }; see Error codes for code.

js
await fb2k.invoke('port.postMessageTo', { portId: 'port_00000001', targetPortId: 'port_00000002', message: { text: 'sync' } });

PortHub events ​

EventTriggerPayload fields
port:connectedPort createdportId, name, windowId
port:disconnectedPort destroyed or window cleaned upportId, name, windowId
port:messageMessage received on portportId, sourcePortId, sourceWindowId, message
state:changedstate.set (non-silent)key, value, previousValue, sourceWindowId, expiresAt?
state:deletedstate.delete or TTL expirykey, sourceWindowId, reason

Routing and PortHub events ​

  • port.connect binds the new port to the invoking window. Only that owner may disconnect it or send through it; port.postMessage excludes the sending port and routes port:message to peer ports on the same name.
  • Public PortHub events are port:connected, port:disconnected, port:message, state:changed, and state:deleted.