Skip to content

Commit aaca1ad

Browse files
authored
feat: appsync events (#8059)
1 parent e485b59 commit aaca1ad

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 this feature, you can build multi-user features such as a collaborative document editors, chat apps, and live polling systems.
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+
## Connect to an Event API without an existing Amplify backend
37+
38+
Before you begin, you will need:
39+
40+
- An Event API created via the AWS Console
41+
- Take note of: HTTP endpoint, region, API Key
42+
43+
```ts
44+
import { useState, useEffect } from 'react';
45+
import { Amplify } from 'aws-amplify';
46+
import { events } from 'aws-amplify/data';
47+
48+
Amplify.configure({
49+
API: {
50+
Events: {
51+
endpoint: 'https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-east-1.amazonaws.com/event',
52+
region: 'us-east-1',
53+
defaultAuthMode: 'apiKey',
54+
apiKey: 'da2-abcdefghijklmnopqrstuvwxyz',
55+
},
56+
},
57+
});
58+
59+
export default function MyComponent() {
60+
const [events, setEvents] = useState<any[]>([]);
61+
62+
useEffect(() => {
63+
let channel;
64+
65+
const connectAndSubscribe = async () => {
66+
channel = await events.connect('default/channel');
67+
68+
channel.subscribe({
69+
next: (data) => {
70+
console.log('received', data);
71+
setEvents(prev => [data, ...prev]);
72+
},
73+
error: (err) => console.error('error', err),
74+
});
75+
}
76+
77+
connectAndSubscribe();
78+
79+
return () => channel && channel.close();
80+
}, []);
81+
82+
async function publishEvent() {
83+
await events.post("default/channel", {some: 'data'});
84+
}
85+
86+
return (
87+
<button onClick={publishEvent}>Publish Event</button>
88+
<ul>
89+
{events.map((event) => (
90+
<li>{JSON.stringify(event)}</li>
91+
))}
92+
</ul>
93+
);
94+
}
95+
```
96+
97+
## Connect to an Event API with an existing Amplify backend
98+
99+
Coming Soon

0 commit comments

Comments
 (0)