Flexible Communication
Choose AJAX, SSE, or WebSocket per page—the right tool for each job.
AJAX
Simple request-response. User triggers an action, server responds.
AJAX + SSE
AJAX for client-to-server, SSE for server push. Best of both worlds.
WebSocket
Full bidirectional real-time communication. True instant updates.
Communication Modes Explained
AJAX Mode
The simplest and most efficient mode for typical web applications.
- User triggers event (click, form submit, etc.)
- Request sent to server via AJAX
- Server processes event and returns UI updates
- Browser applies updates to the DOM
Best for: Forms, CRUD operations, content pages, admin panels
AJAX + SSE Mode
Combines AJAX requests with Server-Sent Events for server push.
- AJAX for client-to-server communication
- Persistent SSE connection for server push
- Server can update UI at any time
- Automatic reconnection if connection drops
Best for: Dashboards, notifications, live feeds, monitoring
WebSocket Mode
Full bidirectional communication over a persistent connection.
- Single persistent connection
- Instant bidirectional messaging
- Lowest latency possible
- Efficient for high-frequency updates
Best for: Chat apps, collaborative editing, gaming, trading platforms
Configuration
By default, all pages use AJAX + SSE mode, which provides a good balance of simplicity and real-time capability. You can change this default application-wide or override it on a page-by-page basis.
Changing the Application Default
Set the default communication mode for all pages in your Application class.
@WebListener
public class MyApplication extends Application
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
super.contextInitialized(sce);
// Change the default mode to AJAX only (no SSE connection)
setCommunicationMode(CommunicationMode.AJAX_ONLY);
// Or use WebSocket as the default
setCommunicationMode(CommunicationMode.WEBSOCKET);
}
}Per-Page Override
Override the default on individual pages by calling setCommunicationMode() in your page's constructor or initialization.
// Simple form page - use AJAX only (no SSE overhead)
@Page("/contact")
public class ContactPage extends HtmlPage
{
public ContactPage()
{
setCommunicationMode(CommunicationMode.AJAX_ONLY);
}
}
// Dashboard with live updates - use AJAX + SSE for server push
@Page("/dashboard")
public class DashboardPage extends HtmlPage
{
public DashboardPage()
{
setCommunicationMode(CommunicationMode.AJAX_WITH_SSE);
}
// Server can push updates anytime
public void updateMetrics(Metrics metrics)
{
metricsPanel.update(metrics);
sendUpdate(); // Push changes to browser
}
}
// Chat application - use WebSocket for real-time messaging
@Page("/chat")
public class ChatPage extends HtmlPage
{
public ChatPage()
{
setCommunicationMode(CommunicationMode.WEBSOCKET);
}
// Instant bidirectional messaging
public void onMessageReceived(ChatMessage message)
{
chatArea.addMessage(message);
sendUpdate(); // Instant delivery
}
}Choosing the Right Mode
For most applications, AJAX + SSE is the ideal choice. It provides server push capability for real-time updates while using efficient AJAX for client-to-server communication. Use WebSocket when you need true bidirectional real-time messaging.
When to Use AJAX Only
While AJAX + SSE is the best mode for most applications, there are scenarios where AJAX_ONLY is the better choice:
High-Traffic Sites
SSE maintains a persistent connection per client. For sites with many concurrent users where server push isn't needed, AJAX_ONLY reduces server resource usage.
Simple Request/Response Pages
Contact forms, login pages, and basic CRUD operations don't need server push. AJAX_ONLY eliminates unnecessary connection overhead.
Serverless Deployments
Some serverless platforms or containerized environments don't handle long-lived connections well. AJAX_ONLY works in any environment.
Simple Load Balancing
SSE connections require sticky sessions or connection-aware routing. AJAX_ONLY works with any load balancer configuration.
Restrictive Networks
Some corporate proxies or firewalls have issues with persistent connections. AJAX_ONLY works through any network configuration.
Mobile Battery Life
Persistent connections increase battery drain on mobile devices. AJAX_ONLY is more power-efficient for mobile users.