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
possiblePortsArray Of Numbers (Required)
This will start a WebSocket server on the first available port.tokenString (Required)
Your provided security token, used to validate clients before allowing a connection.
Returns
successBoolean
If the action was successful.portNumber
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
successBoolean
If the action was successful.messageString
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
messageString
The message to broadcast.
Returns
successBoolean
If the action was successful.messageString
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
clientIdString
The client to send the message to.messageString
The message to send.
Returns
successBoolean
If the action was successful.messageString
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
isRunningBoolean
If the server is currently running.clientIdsArray Of Strings
A list of connected client ids.portNumber
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
idString
The ID of the client.
Client Disconnected
Fires when a client disconnects either gracefully or unexpectedly.
window.deskifier.webSocket.onClientDisconnected((data) => { })
Arguments
idString
The ID of the client.
Web Socket Message
Fires when a client sends a message to the server.
window.deskifier.webSocket.onMessage((data) => { })
Arguments
idString
The ID of the client.contentString
The message content.
<br>