# 4NK Protocol Server A high-availability server that automatically handles protocol operations for the 4NK network. ## Features - **High Availability**: Runs continuously on a server - **Automatic Operations**: Handles UPDATE_PROCESS, NOTIFY_UPDATE, and VALIDATE_STATE operations - **Protocol Compatible**: Uses the same message format as the browser client - **WebSocket Interface**: Real-time communication with clients ## Quick Start ### 1. Install Dependencies ```bash npm install ``` ### 2. Build the Server ```bash npm run build:server ``` ### 3. Start the Server ```bash npm run start:server ``` ### 4. Development Mode ```bash npm run dev:server ``` ## Configuration Create a `.env` file in the root directory: ```env PORT=8080 JWT_SECRET_KEY=your-secret-key-here DATABASE_PATH=./data/server.db RELAY_URLS=ws://localhost:8090,ws://relay2.example.com:8090 LOG_LEVEL=info ``` ## API Usage Connect to the WebSocket server and send messages in the same format as the browser client: ### Example: Update Process ```javascript const ws = new WebSocket('ws://localhost:8080'); ws.onopen = () => { ws.send(JSON.stringify({ type: 'UPDATE_PROCESS', processId: 'your-process-id', newData: { field: 'value' }, privateFields: [], roles: {}, accessToken: 'your-access-token', messageId: 'unique-message-id' })); }; ws.onmessage = (event) => { const response = JSON.parse(event.data); console.log('Response:', response); }; ``` ### Example: Notify Update ```javascript ws.send(JSON.stringify({ type: 'NOTIFY_UPDATE', processId: 'your-process-id', stateId: 'your-state-id', accessToken: 'your-access-token', messageId: 'unique-message-id' })); ``` ### Example: Validate State ```javascript ws.send(JSON.stringify({ type: 'VALIDATE_STATE', processId: 'your-process-id', stateId: 'your-state-id', accessToken: 'your-access-token', messageId: 'unique-message-id' })); ``` ## Deployment ### Systemd Service (Linux) ```bash # Create service file sudo tee /etc/systemd/system/4nk-server.service > /dev/null <