-
Notifications
You must be signed in to change notification settings - Fork 0
WebBridge Guide
WebBridge provides a powerful system for integrating FastPlot with web applications through a TCP-based communication protocol. It enables real-time data streaming, dashboard configuration synchronization, and remote control capabilities.
WebBridge serves as a communication bridge between MATLAB/FastPlot and web clients, using NDJSON (Newline Delimited JSON) messages over TCP. This architecture allows:
- Real-time streaming of sensor data to web applications
- Bidirectional dashboard configuration synchronization
- Remote action execution from web interfaces
- Multiple concurrent client connections
% Create a dashboard with some data
dashboard = Dashboard();
dashboard.addSensor('temperature', randn(1000, 1), 'units', '°C');
% Create and start WebBridge
bridge = WebBridge(dashboard);
bridge.serve(); % Starts both TCP server and data polling% Customize polling interval for configuration changes
bridge = WebBridge(dashboard, 'ConfigPollInterval', 0.5); % Poll every 500ms
% Manual control over TCP server
bridge = WebBridge(dashboard);
bridge.startTcp(); % Start TCP server only
% ... later ...
bridge.stop(); % Stop all servicesWebBridge uses a structured message protocol for communication:
When a client connects, WebBridge sends initialization data:
% The bridge automatically sends:
% - Signal definitions (names, units, data types)
% - Dashboard configuration (layouts, themes, etc.)
% - Available actions listAs data changes, WebBridge streams updates:
% Manually trigger data change notifications
bridge.notifyDataChanged('temperature'); % Single signal
bridge.notifyDataChanged({'temp', 'pressure'}); % Multiple signalsDashboard configuration changes are automatically detected and broadcast:
% Any changes to dashboard properties trigger config updates
dashboard.Title = 'Updated Dashboard';
% WebBridge automatically detects and broadcasts this changeRegister callback functions that can be invoked from web clients:
% Register a simple action
bridge.registerAction('reset_data', @() resetSensorData());
% Register an action with parameters
bridge.registerAction('set_threshold', @(params) setThreshold(params.value));
% Register an action that returns results
bridge.registerAction('get_stats', @() struct('mean', mean(data), 'std', std(data)));% Check if an action exists
if bridge.hasAction('reset_data')
disp('Reset action is available');
end
% Actions are automatically broadcast to clients when registereddashboard = Dashboard();
sensor = dashboard.addSensor('live_data', []);
bridge = WebBridge(dashboard);
bridge.serve();
% Simulate live data updates
timer_obj = timer('ExecutionMode', 'fixedRate', 'Period', 0.1, ...
'TimerFcn', @(~,~) updateLiveData());
function updateLiveData()
new_data = randn(10, 1);
sensor.appendData(new_data);
bridge.notifyDataChanged('live_data');
end
start(timer_obj);% WebBridge can handle complex dashboard configurations
dashboard = Dashboard();
dashboard.addSensor('sensor1', data1);
dashboard.addSensor('sensor2', data2);
% All sensors and their configurations are automatically synchronized
bridge = WebBridge(dashboard);
bridge.serve();% Register actions with error handling
bridge.registerAction('process_data', @processDataHandler);
function result = processDataHandler(params)
try
% Process the request
result = struct('success', true, 'data', processedData);
catch ME
result = struct('success', false, 'error', ME.message);
end
endAll messages use NDJSON format (one JSON object per line):
% Example message types sent by WebBridge:
% {"type": "init", "signals": [...], "dashboard": {...}, "actions": [...]}
% {"type": "data_changed", "signalIds": ["sensor1", "sensor2"]}
% {"type": "config_changed", "dashboard": {...}}
% {"type": "actions_changed", "actionNames": ["action1", "action2"]}
% {"type": "action_result", "requestId": "123", "name": "action1", "ok": true, "data": {...}}Web clients should handle these message types:
-
init: Initial data and configuration -
data_changed: Signal data updates -
config_changed: Dashboard configuration updates -
actions_changed: Available actions list updates -
action_result: Results from action execution
% Batch multiple signal updates
bridge.notifyDataChanged({'temp', 'pressure', 'humidity'});
% Rather than individual notifications:
% bridge.notifyDataChanged('temp');
% bridge.notifyDataChanged('pressure');
% bridge.notifyDataChanged('humidity');% Adjust polling frequency based on needs
bridge.ConfigPollInterval = 2.0; % Less frequent for stable configurations
bridge.ConfigPollInterval = 0.1; % More frequent for dynamic dashboards% WebBridge handles client connections automatically
% Multiple clients can connect simultaneously
% Disconnected clients are automatically cleaned up% Action errors are automatically captured and sent to clients
bridge.registerAction('failing_action', @() error('Something went wrong'));
% Client receives: {"ok": false, "error": "Something went wrong"}WebBridge works seamlessly with the Dashboard Engine Guide:
% Dashboard engine changes are automatically synchronized
dashboard.Theme = 'dark';
dashboard.Layout = 'grid';
% WebBridge detects and broadcasts these changes- Register all actions before starting the server to ensure clients receive the complete actions list
- Use batch notifications for multiple simultaneous data updates
- Handle action errors gracefully with try-catch blocks
- Set appropriate polling intervals based on configuration change frequency
-
Clean up resources by calling
stop()when shutting down
For more information on dashboard configuration, see the Dashboard page.
FastPlot Wiki
API Reference
Guides
Use Cases
Internals
Resources