Skip to content

Commit 88a484c

Browse files
committed
feat: appsync events
1 parent e209607 commit 88a484c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/directory/directory.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ export const directory = {
320320
{
321321
path: 'src/pages/[platform]/build-a-backend/data/optimistic-ui/index.mdx'
322322
},
323+
{
324+
path: 'src/pages/[platform]/build-a-backend/data/connect-event-api/index.mdx'
325+
},
323326
{
324327
path: 'src/pages/[platform]/build-a-backend/data/override-resources/index.mdx'
325328
},
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { getCustomStaticPath } from '@/utils/getCustomStaticPath';
2+
3+
export const meta = {
4+
title: 'Connect to AWS AppSync Events',
5+
description:
6+
'Connect to AWS AppSync Events',
7+
platforms: [
8+
'angular',
9+
'javascript',
10+
'nextjs',
11+
'react',
12+
'react-native',
13+
'vue'
14+
]
15+
};
16+
17+
export const getStaticPaths = async () => {
18+
return getCustomStaticPath(meta.platforms);
19+
};
20+
21+
export function getStaticProps(context) {
22+
return {
23+
props: {
24+
platform: context.params.platform,
25+
meta
26+
}
27+
};
28+
}
29+
30+
This guide walks through how you can connect to AWS AppSync Events using the Amplify library.
31+
32+
AWS AppSync Events lets you create secure and performant serverless WebSocket APIs that can broadcast real-time event data to millions of subscribers, without you having to manage connections or resource scaling. With AWS AppSync Events, there is no API code required to get started, so you can create production-ready real-time web and mobile experiences in minutes.
33+
34+
Learn more about AWS AppSync Events by visiting the [Developer Guide](https://docs.aws.amazon.com/appsync/latest/eventapi/event-api-welcome.html).
35+
36+
## Quickstart
37+
### Connect to an Event API without an existing Amplify application
38+
39+
Before you begin, you will need:
40+
41+
- An Event API created via the AWS Console, CDK, CLI, etc.
42+
- Take note of: HTTP endpoint, region, API Key
43+
44+
```ts
45+
import { useState, useEffect } from 'react';
46+
import { Amplify } from 'aws-amplify';
47+
import { events } from 'aws-amplify/data';
48+
49+
Amplify.configure({
50+
API: {
51+
Events: {
52+
endpoint: 'https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-east-1.amazonaws.com/event',
53+
region: 'us-east-1',
54+
defaultAuthMode: 'apiKey',
55+
apiKey: 'da2-abcdefghijklmnopqrstuvwxyz',
56+
},
57+
},
58+
});
59+
60+
export default function MyComponent() {
61+
const [events, setEvents] = useState<any[]>([]);
62+
63+
useEffect(() => {
64+
let channel;
65+
66+
const connectAndSubscribe = async () => {
67+
channel = await events.connect('default/channel');
68+
69+
channel.subscribe({
70+
next: (data) => {
71+
console.log('received', data);
72+
setEvents(prev => [data, ...prev]);
73+
},
74+
error: (err) => console.error('error', err),
75+
});
76+
}
77+
78+
connectAndSubscribe();
79+
80+
return () => channel && channel.close();
81+
}, []);
82+
83+
async function publishEvent() {
84+
await events.post("default/channel", {some: 'data'});
85+
}
86+
87+
return (
88+
<button onClick={publishEvent}>Publish Event</button>
89+
<ul>
90+
{events.map((event) => (
91+
<li>{JSON.stringify(event)}</li>
92+
))}
93+
</ul>
94+
);
95+
}
96+
```
97+
98+
## Connect an Event API to an existing Amplify application
99+
100+
Coming Soon

0 commit comments

Comments
 (0)