-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: appsync events #8059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: appsync events #8059
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/pages/[platform]/build-a-backend/data/connect-event-api/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; | ||
|
||
export const meta = { | ||
title: 'Connect to AWS AppSync Events', | ||
description: | ||
'Connect to AWS AppSync Events', | ||
platforms: [ | ||
'angular', | ||
'javascript', | ||
'nextjs', | ||
'react', | ||
'react-native', | ||
'vue' | ||
] | ||
}; | ||
|
||
export const getStaticPaths = async () => { | ||
return getCustomStaticPath(meta.platforms); | ||
}; | ||
|
||
export function getStaticProps(context) { | ||
return { | ||
props: { | ||
platform: context.params.platform, | ||
meta | ||
} | ||
}; | ||
} | ||
|
||
This guide walks through how you can connect to AWS AppSync Events using the Amplify library. | ||
|
||
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. | ||
|
||
Learn more about AWS AppSync Events by visiting the [Developer Guide](https://docs.aws.amazon.com/appsync/latest/eventapi/event-api-welcome.html). | ||
|
||
## Connect to an Event API without an existing Amplify backend | ||
|
||
Before you begin, you will need: | ||
|
||
- An Event API created via the AWS Console | ||
- Take note of: HTTP endpoint, region, API Key | ||
|
||
```ts | ||
import { useState, useEffect } from 'react'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { events } from 'aws-amplify/data'; | ||
|
||
Amplify.configure({ | ||
API: { | ||
Events: { | ||
endpoint: 'https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-east-1.amazonaws.com/event', | ||
region: 'us-east-1', | ||
defaultAuthMode: 'apiKey', | ||
apiKey: 'da2-abcdefghijklmnopqrstuvwxyz', | ||
}, | ||
}, | ||
}); | ||
|
||
export default function MyComponent() { | ||
const [events, setEvents] = useState<any[]>([]); | ||
|
||
useEffect(() => { | ||
let channel; | ||
|
||
const connectAndSubscribe = async () => { | ||
channel = await events.connect('default/channel'); | ||
|
||
channel.subscribe({ | ||
next: (data) => { | ||
console.log('received', data); | ||
setEvents(prev => [data, ...prev]); | ||
}, | ||
error: (err) => console.error('error', err), | ||
}); | ||
} | ||
|
||
connectAndSubscribe(); | ||
|
||
return () => channel && channel.close(); | ||
}, []); | ||
|
||
async function publishEvent() { | ||
await events.post("default/channel", {some: 'data'}); | ||
} | ||
|
||
return ( | ||
<button onClick={publishEvent}>Publish Event</button> | ||
<ul> | ||
{events.map((event) => ( | ||
<li>{JSON.stringify(event)}</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
``` | ||
|
||
## Connect to an Event API with an existing Amplify backend | ||
|
||
Coming Soon | ||
iartemiev marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this title: "Real time events"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a separate page with that verbiage https://docs.amplify.aws/react/build-a-backend/data/subscribe-data/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about Pub/Sub events?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that conflicts with the pub/sub category we'll be deprecating - https://docs.amplify.aws/gen1/javascript/build-a-backend/more-features/pubsub/