WebSocket Server

This exposes a local WebSocket server that allows communication between your web app running in a browser, and your desktop app.

You generally will only have one client (the browser tab) but if multiple tabs are open and requesting a connection, you'll need to handle multiple connections.

Example code in the browser:

const socket = new WebSocket('wss://local.deskifier.com:50050?token=YOUR_SECURE_KEY'); // Replace 50050 with the actual port your server is using

socket.onopen = () => {
  console.log('Connected to WebSocket server');
};

socket.onmessage = (event) => {
  console.log('Message from app:', event.data);
};

// Event: Connection closed
socket.onclose = () => {
  console.log('WebSocket connection closed');
};

socket.send('Hello from the browser!');

Use this URL as your connection string

wss://local.deskifier.com

This URL will resolve to 127.0.0.1

Methods

Start Server

Attempts to start a WebSocket server on one of the given ports.

Security Warning:

When enabled, the WebSocket server opens a port that any reachable webpage or application can connect to (including browsers, local apps, and possibly other devices on your network).

All incoming requests should be treated as untrusted. You should validate and authorize messages before using them.

await window.deskifier.webSocket.startServer({ arguments })

Arguments

  • possiblePorts Array Of Numbers (Required)
    This will start a WebSocket server on the first available port.
  • token String (Required)
    Your provided security token, used to validate clients before allowing a connection.

Returns

  • success Boolean
    If the action was successful.
  • port Number
    The port used.

Example

const result = await window.deskifier.webSocket.startServer({ 
    possiblePorts: [50050, 50060],
    token: "YOUR_SECURE_KEY" 
});

console.log(result.port) //50050

Stop Server

Stops the running WebSocket server.<br>

await window.deskifier.webSocket.stopServer()

Returns

  • success Boolean
    If the action was successful.
  • message String
    Additional confirmation, or error details if action was unsuccessful.

Broadcast Message

Sends a message to all connected clients.<br>

This will not return an error if one of the clients aren't able to receive the message. If you want to ensure deliverability, use the Send Message method.

await window.deskifier.webSocket.broadcast({ arguments })

Arguments

  • message String
    The message to broadcast.

Returns

  • success Boolean
    If the action was successful.
  • message String
    Additional confirmation, or error details if action was unsuccessful.

Send Message

Sends a message to a specific client.<br>

await window.deskifier.webSocket.send({ arguments })

Arguments

  • clientId String
    The client to send the message to.
  • message String
    The message to send.

Returns

  • success Boolean
    If the action was successful.
  • message String
    Additional confirmation, or error details if action was unsuccessful.

Properties

Get Server

Retrieves the running server details (if any)<br>

await window.deskifier.webSocket.getServer()

Returns

  • isRunning Boolean
    If the server is currently running.
  • clientIds Array Of Strings
    A list of connected client ids.
  • port Number
    The port the server is currently running on (null if not running)

Events

Client Connected

Fires when a client connects.

window.deskifier.webSocket.onClientConnected((data) => { })

Arguments

  • id String
    The ID of the client.

Client Disconnected

Fires when a client disconnects either gracefully or unexpectedly.

window.deskifier.webSocket.onClientDisconnected((data) => { })

Arguments

  • id String
    The ID of the client.

Web Socket Message

Fires when a client sends a message to the server.

window.deskifier.webSocket.onMessage((data) => { })

Arguments

  • id String
    The ID of the client.
  • content String
    The message content.

<br>