A real-time chat application deployed as a standard WAR file. Uses @ManagedService with WebSocket and automatic long-polling fallback.
@ManagedServiceannotation-driven handler with@Ready,@Disconnect,@Heartbeat,@Message- Jackson encoding/decoding via
@Message(encoders = ..., decoders = ...) - Dependency injection of
BroadcasterFactory,AtmosphereResource,Broadcaster - WAR packaging with
web.xmlconfiguration - WebSocket with transparent long-polling fallback
The entire server is one annotated class:
@ManagedService(path = "/chat", atmosphereConfig = MAX_INACTIVE + "=120000")
public class Chat {
@Inject private BroadcasterFactory factory;
@Inject private AtmosphereResource r;
@Ready
public void onReady() { /* client connected */ }
@Disconnect
public void onDisconnect() { /* client left */ }
@Message(encoders = {JacksonEncoder.class}, decoders = {JacksonDecoder.class})
public Message onMessage(Message message) {
return message; // returning broadcasts to all subscribers
}
}Registers AtmosphereServlet with annotation scanning:
<servlet>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.cpr.packages</param-name>
<param-value>org.atmosphere.samples</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>Vanilla JavaScript using atmosphere.js 5.0 (no framework dependencies):
- Subscribes to
/chatwith WebSocket transport and long-polling fallback - On first connection, prompts the user for a name
- Sends JSON messages:
{ author, message } - Receives and displays messages with timestamps and author attribution
subscription = await atmosphere.atmosphere.subscribe(
{ url: '/chat', transport: 'websocket', fallbackTransport: 'long-polling' },
{
open: (res) => { /* show connected status */ },
message: (res) => { /* parse JSON, display message */ },
close: () => { /* show disconnected */ },
}
);
subscription.push(JSON.stringify({ author: 'Alice', message: 'Hello!' }));# Build
mvn clean install
# Run with embedded Jetty via Maven plugin
mvn jetty:runOpen http://localhost:8080/atmosphere-chat/ in multiple browser tabs to chat.
Build the WAR and deploy to any Servlet 6.0+ container:
mvn clean package
# Deploy target/atmosphere-chat.war to Tomcat, GlassFish, Jetty, etc.chat/
├── pom.xml # WAR packaging
└── src/main/
├── java/org/atmosphere/samples/chat/
│ ├── Chat.java # @ManagedService handler
│ ├── Message.java # Message POJO
│ ├── JacksonEncoder.java # Message → JSON
│ └── JacksonDecoder.java # JSON → Message
└── webapp/
├── WEB-INF/web.xml # AtmosphereServlet config
├── index.html # Chat UI
└── assets/ # Bundled atmosphere.js + chat client