-
-
Notifications
You must be signed in to change notification settings - Fork 761
Understanding AtmosphereResource
jfarcand edited this page Feb 18, 2026
·
22 revisions
Advanced topic. Most applications use
@ManagedServicewith@Injectand don't need to interact withAtmosphereResourcedirectly.
An AtmosphereResource represents a single client connection. It wraps the underlying HTTP request/response and provides methods for suspending, resuming, and writing to the connection.
@ManagedService(path = "/chat") public class Chat { @Inject private AtmosphereResource r; @Ready public void onReady() { String uuid = r.uuid(); // Unique connection ID String transport = r.transport().name(); // WEBSOCKET, SSE, LONG_POLLING, etc. } }
@Ready public void onReady(AtmosphereResource r) { // r is the current resource }
r.uuid(); // Unique connection identifier r.transport(); // Transport type (WEBSOCKET, SSE, LONG_POLLING, etc.) r.suspend(); // Suspend the connection (keep it open) r.resume(); // Resume and complete the response r.close(); // Close the connection r.getRequest(); // AtmosphereRequest (wraps HttpServletRequest) r.getResponse(); // AtmosphereResponse (wraps HttpServletResponse) r.getAtmosphereConfig(); // Framework configuration r.getBroadcaster(); // The Broadcaster this resource is attached to r.setBroadcaster(b); // Change broadcaster r.addEventListener(listener); // Add lifecycle event listener r.session(); // Get/create AtmosphereResourceSession
Injected or passed to @Disconnect handlers:
@Inject private AtmosphereResourceEvent event; @Disconnect public void onDisconnect() { event.isCancelled(); // Unexpected disconnect (e.g., network error) event.isClosedByClient(); // Client closed intentionally event.isClosedByApplication(); // Server closed the connection event.getResource(); // The AtmosphereResource event.getMessage(); // Last message (if any) }
For programmatic lifecycle tracking (alternative to annotations):
r.addEventListener(new AtmosphereResourceEventListenerAdapter() { @Override public void onSuspend(AtmosphereResourceEvent event) { // Connection suspended } @Override public void onDisconnect(AtmosphereResourceEvent event) { // Client disconnected } @Override public void onBroadcast(AtmosphereResourceEvent event) { // Message broadcast to this resource } @Override public void onHeartbeat(AtmosphereResourceEvent event) { // Heartbeat received } });
Convenience inner classes are available for single-event listeners:
r.addEventListener(new AtmosphereResourceEventListenerAdapter.OnDisconnect() { @Override public void onDisconnect(AtmosphereResourceEvent event) { // Only handle disconnect } });