Training Day

Real-Time Communication

Implementing real-time data exchange with FileMaker

Real-time communication enables systems to exchange data with minimal latency, providing immediate updates when data changes occur.

Real-Time Communication Overview

Unlike batch processes or scheduled synchronization, real-time communication focuses on:

  • Immediate data exchange (milliseconds to seconds)
  • Event-driven interactions
  • Live user experiences
  • Bi-directional data flow

Real-Time Transport Options

WebSockets

WebSockets provide a persistent connection between client and server:

  • Full-duplex: Data flows in both directions simultaneously
  • Persistent connection: Maintains a single TCP connection
  • Low latency: Minimal overhead after initial handshake
  • Browser support: Native in modern browsers
  • FileMaker integration: Requires custom web viewer or plugin

Example of WebSocket connection (JavaScript in web viewer):

// JavaScript code inside FileMaker web viewer
const socket = new WebSocket('wss://example.com/socket');
 
socket.onopen = function (e) {
  console.log('Connection established');
};
 
socket.onmessage = function (event) {
  // Parse incoming data
  const data = JSON.parse(event.data);
 
  // Send data to FileMaker
  FileMaker.PerformScript('Process WebSocket Data', JSON.stringify(data));
};
 
// Send message from FileMaker
function sendMessage(message) {
  socket.send(JSON.stringify(message));
}

HTTP Long Polling

Long polling is a technique that simulates real-time through extended HTTP requests:

  • Compatibility: Works with standard HTTP tools in FileMaker
  • Simulated push: Server holds request open until data is available
  • Implementation: Can be implemented with Insert From URL
  • Drawbacks: Higher overhead than WebSockets

FileMaker implementation:

# Long polling script
Set Variable [$url; Value: "https://api.example.com/poll"]
Set Variable [$options; Value: "-X GET -m 180"] # 3-minute timeout
Insert From URL [Select; $result; $url; cURL options: $options]
If [not IsEmpty($result)]
  # Process results
  ...
  # Immediately make next request
  Perform Script [Self]
End If

Server-Sent Events (SSE)

One-way communication channel from server to client:

  • Simple: Built on standard HTTP
  • Efficient: Designed for server-to-client streaming
  • Auto-reconnect: Built-in reconnection handling
  • FileMaker limitation: Not directly supported in FileMaker

Polling Techniques

When true real-time isn't available, polling provides an alternative:

Standard Polling

Simplest approach - regular checks for updates:

# Basic polling script
Set Variable [$interval; Value: 30] # Seconds between checks

While [True]
  # Check for new data
  Insert From URL [Select; $result; "https://api.example.com/updates?since=" & $lastCheck]
  # Process results
  ...
  # Wait for next check
  Pause/Resume Script [Duration (seconds): $interval]
End Loop

Intelligent Polling

Adaptive polling that changes frequency based on activity:

# Start with default interval
Set Variable [$interval; Value: 30]
Set Variable [$maxInterval; Value: 300]  # 5 minutes
Set Variable [$minInterval; Value: 5]    # 5 seconds

While [True]
  # Check for new data
  Insert From URL [Select; $result; "https://api.example.com/updates?since=" & $lastCheck]

  # Adjust interval based on result
  If [not IsEmpty(JSONGetElement($result; "data"))]
    # Data received, poll more frequently
    Set Variable [$interval; Value: $minInterval]
  Else
    # No data, gradually back off
    Set Variable [$interval; Value: Min($interval * 1.5; $maxInterval)]
  End If

  # Wait for next check
  Pause/Resume Script [Duration (seconds): $interval]
End Loop

Implementation Approaches in FileMaker

Plugin-Based Solutions

Several FileMaker plugins offer WebSocket support:

  • MBS Plugin: Provides WebSocket client functionality
  • Zippscript: Offers WebSocket capabilities
  • BaseElements: Includes web communication features

Example with MBS Plugin:

# Create WebSocket connection
Set Variable [$socket; Value: MBS("WebSocket.New"; "wss://example.com/socket")]

# Set up event handlers
Set Variable [$result; Value: MBS("WebSocket.SetMessageCallback"; $socket; "WebSocket_OnMessage")]
Set Variable [$result; Value: MBS("WebSocket.SetOpenCallback"; $socket; "WebSocket_OnOpen")]
Set Variable [$result; Value: MBS("WebSocket.SetCloseCallback"; $socket; "WebSocket_OnClose")]

# Connect
Set Variable [$result; Value: MBS("WebSocket.Connect"; $socket)]

Web Viewer Integration

Leverage web viewers to handle real-time communication:

  1. Create a web viewer with custom HTML/JavaScript
  2. Use JavaScript for WebSocket or SSE connections
  3. Bridge data to FileMaker using the Perform Script JavaScript function
  4. Implement a FileMaker script to receive and process data

Server-Side Component

Implement a middleware layer that bridges your real-time service and FileMaker:

  1. Create a server application (Node.js, Python, etc.) that manages WebSocket connections
  2. Configure the server to interact with FileMaker via the Data API
  3. Have FileMaker regularly check for updates via the server application

Common Real-Time Integration Scenarios

Chat and Messaging

  • Customer support chat: Live interaction with customers
  • Team collaboration: Internal messaging between staff
  • Status updates: System notifications and alerts

Live Dashboard Updates

  • Sales statistics: Real-time revenue and conversion tracking
  • Production metrics: Live manufacturing or service delivery stats
  • System monitoring: Server health and performance metrics

Collaborative Editing

  • Document collaboration: Multiple users editing the same record
  • Approval workflows: Live status updates during review processes

Performance and Scaling Considerations

  1. Connection management: Monitor and manage persistent connections
  2. Resource utilization: WebSockets consume server resources
  3. Error handling: Implement robust reconnection strategies
  4. Load balancing: Consider connection distribution across servers
  5. Message prioritization: Prioritize critical real-time messages