starti.app
API Reference

Network

Monitor network connectivity and communicate over UDP on the local network. Useful for detecting online/offline state and discovering local devices.

Access: startiapp.Network

Methods

currentConnectionState(): Promise<ConnectionChangeEvent>

Returns the current network connectivity state, including the type of network access and the active connection profile.

Returns: Promise<ConnectionChangeEvent> —The current connection state.

Example:

const state = await startiapp.Network.currentConnectionState();
console.log("Network access:", state.networkAccess);
console.log("Connection:", state.connectionProfiles);

startListeningForConnectionChanges(): Promise<void>

Starts monitoring for changes in network connectivity. Once started, the connectionStateChanged event fires whenever the connection state changes.

Example:

startiapp.Network.addEventListener("connectionStateChanged", (event) => {
  const { networkAccess, connectionProfiles } = event.detail;
  console.log(`Network changed: ${networkAccess} via ${connectionProfiles}`);
});

await startiapp.Network.startListeningForConnectionChanges();

stopListeningForConnectionChanges(): Promise<void>

Stops monitoring for network connectivity changes.

Example:

await startiapp.Network.stopListeningForConnectionChanges();

sendUdpBroadcast(port: number, message: string): Promise<void>

Sends a UDP broadcast message on the local network.

Parameters:

ParameterTypeRequiredDescription
portnumberYesThe UDP port to broadcast on.
messagestringYesThe message payload to send.

Example:

await startiapp.Network.sendUdpBroadcast(9000, "DISCOVER");

startListeningForUdpPackets(port: number): Promise<void>

Starts listening for incoming UDP packets on the specified port. Received packets fire the udpPacketReceived event.

Parameters:

ParameterTypeRequiredDescription
portnumberYesThe UDP port to listen on.

Example:

startiapp.Network.addEventListener("udpPacketReceived", (event) => {
  const { message, ip } = event.detail.detail;
  console.log(`Received from ${ip}: ${message}`);
});

await startiapp.Network.startListeningForUdpPackets(9000);

stopListeningForUdpPackets(): Promise<void>

Stops listening for UDP packets.

Example:

await startiapp.Network.stopListeningForUdpPackets();

Events

connectionStateChanged

Fired when the network connectivity state changes. You must call startListeningForConnectionChanges() first.

Event data: ConnectionChangeEvent

{
  networkAccess: NetworkAccess;
  connectionProfiles: ConnectionProfile;
}

Example:

startiapp.Network.addEventListener("connectionStateChanged", (event) => {
  if (event.detail.networkAccess === "None") {
    console.log("Device is offline");
  }
});

await startiapp.Network.startListeningForConnectionChanges();

udpPacketReceived

Fired when a UDP packet is received on the listened port. You must call startListeningForUdpPackets() first.

Event data: UdpPacketReceivedEvent

{
  detail: {
    message: string;
    ip: string;
  }
}

Example:

startiapp.Network.addEventListener("udpPacketReceived", (event) => {
  const { message, ip } = event.detail.detail;
  console.log(`UDP packet from ${ip}: ${message}`);
});

await startiapp.Network.startListeningForUdpPackets(9000);

Types

NetworkAccess

Describes the level of network access available.

type NetworkAccess =
  | "ConstrainedInternet"  // Limited internet (e.g. captive portal)
  | "Internet"             // Full internet access
  | "Local"                // Local network only, no internet
  | "None"                 // No network connectivity
  | "Unknown";             // State cannot be determined

ConnectionProfile

Describes the type of active network connection.

type ConnectionProfile =
  | "Bluetooth"
  | "Cellular"
  | "Ethernet"
  | "WiFi"
  | "Unknown";

ConnectionChangeEvent

The payload for the connectionStateChanged event.

type ConnectionChangeEvent = {
  networkAccess: NetworkAccess;
  connectionProfiles: ConnectionProfile;
};

UdpPacketReceivedEvent

The payload for the udpPacketReceived event.

type UdpPacketReceivedEvent = {
  detail: {
    message: string;
    ip: string;
  };
};

Usage Patterns

Detecting online/offline state

// Check current state
const state = await startiapp.Network.currentConnectionState();
const isOnline = state.networkAccess === "Internet";

// Monitor for changes
startiapp.Network.addEventListener("connectionStateChanged", (event) => {
  const isOnline = event.detail.networkAccess === "Internet";
  updateUI(isOnline);
});

await startiapp.Network.startListeningForConnectionChanges();

Local device discovery via UDP

// Listen for responses
startiapp.Network.addEventListener("udpPacketReceived", (event) => {
  const { message, ip } = event.detail.detail;
  console.log(`Device found at ${ip}: ${message}`);
});

await startiapp.Network.startListeningForUdpPackets(9000);

// Broadcast discovery message
await startiapp.Network.sendUdpBroadcast(9000, "DISCOVER");

On this page