Kotlin DSL
Kotlin DSL
Section titled “Kotlin DSL”Builder API and coroutine extensions for Atmosphere.
Maven Coordinates
Section titled “Maven Coordinates”<dependency> <groupId>org.atmosphere</groupId> <artifactId>atmosphere-kotlin</artifactId> <version>LATEST</version> <!-- check Maven Central for latest --></dependency>DSL Builder
Section titled “DSL Builder”Create AtmosphereHandler instances with a type-safe DSL:
import org.atmosphere.kotlin.atmosphere
val handler = atmosphere { onConnect { resource -> println("${resource.uuid()} connected via ${resource.transport()}") } onMessage { resource, message -> resource.broadcaster.broadcast(message) } onDisconnect { resource -> println("${resource.uuid()} left") } onTimeout { resource -> println("${resource.uuid()} timed out") } onResume { resource -> println("${resource.uuid()} resumed") }}
framework.addAtmosphereHandler("/chat", handler)Available callbacks
Section titled “Available callbacks”| Callback | Description |
|---|---|
onConnect | Called when a client connects |
onMessage | Called when a message is received |
onDisconnect | Called when a client disconnects |
onTimeout | Called when a connection times out |
onResume | Called when a suspended connection resumes |
Coroutine Extensions
Section titled “Coroutine Extensions”Suspending versions of blocking Atmosphere methods:
// Suspend instead of blocking on broadcastbroadcaster.broadcastSuspend("Hello!")
// Broadcast to a specific resourcebroadcaster.broadcastSuspend("Private message", resource)
// Suspend instead of blocking on writeresource.writeSuspend("Direct message")resource.writeSuspend(byteArrayOf(0x01, 0x02))These extensions use kotlinx.coroutines to bridge Atmosphere’s Future-based API into structured concurrency.
Samples
Section titled “Samples”- Spring Boot Chat — can be used with Kotlin DSL
See Also
Section titled “See Also”- Core Runtime —
AtmosphereHandler,Broadcaster,AtmosphereResource