Name:
interface
Value:
Extend your Amplify Gen 2 app with AWS Blocks — self-contained backend capabilities you compose into your existing backend.

Page updated Jul 1, 2024

NewDeploy web apps with Git or your AI coding agent using the latest AWS Amplify. Add managed databases and authentication as you grow. Explore the documentation.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade.

Subscribe to real-time events

These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.

Subscribe to mutations for creating real-time clients:

ApiOperation subscription = Amplify.API.subscribe(
ModelSubscription.onCreate(Todo.class),
onEstablished -> Log.i("ApiQuickStart", "Subscription established"),
onCreated -> Log.i("ApiQuickStart", "Todo create subscription received: " + ((Todo) onCreated.getData()).getName()),
onFailure -> Log.e("ApiQuickStart", "Subscription failed", onFailure),
() -> Log.i("ApiQuickStart", "Subscription completed")
);
// Cancel the subscription listener when you're finished with it
subscription.cancel();
val subscription = Amplify.API.subscribe(
ModelSubscription.onCreate(Todo::class.java),
{ Log.i("ApiQuickStart", "Subscription established") },
{ Log.i("ApiQuickStart", "Todo create subscription received: ${(it.data as Todo).name}") },
{ Log.e("ApiQuickStart", "Subscription failed", it) },
{ Log.i("ApiQuickStart", "Subscription completed") }
)
// Cancel the subscription listener when you're finished with it
subscription.cancel();
val job = activityScope.launch {
try {
Amplify.API.subscribe(ModelSubscription.onCreate(Todo::class.java))
.catch { Log.e("ApiQuickStart", "Error on subscription", it) }
.collect { Log.i("ApiQuickStart", "Todo created! ${it.data.name}") }
} catch (notEstablished: ApiException) {
Log.e("ApiQuickStart", "Subscription not established", it)
}
}
// When done with subscription
job.cancel()
RxSubscriptionOperation<? extends GraphQLResponse<?>> subscription =
RxAmplify.API.subscribe(request);
subscription
.observeConnectionState()
.subscribe(connectionStateEvent -> Log.i("ApiQuickStart", String.valueOf(connectionStateEvent)));
subscription
.observeSubscriptionData()
.subscribe(
data -> Log.i("ApiQuickStart", data),
exception -> Log.e("ApiQuickStart", "Subscription failed.", exception),
() -> Log.i("ApiQuickStart", "Subscription completed.")
);
// Cancel the subscription listener when you're finished with it
subscription.cancel();