@@ -41,41 +41,41 @@ Before you begin, you will need:
41
41
- Take note of: HTTP endpoint, region, API Key
42
42
43
43
``` tsx title="src/App.tsx"
44
- import type { EventsChannel } from " aws-amplify/data" ;
45
- import { useState , useEffect , useRef } from " react" ;
46
- import { Amplify } from " aws-amplify" ;
47
- import { events } from " aws-amplify/data" ;
44
+ import type { EventsChannel } from ' aws-amplify/data' ;
45
+ import { useState , useEffect , useRef } from ' react' ;
46
+ import { Amplify } from ' aws-amplify' ;
47
+ import { events } from ' aws-amplify/data' ;
48
48
49
49
Amplify .configure ({
50
50
API: {
51
51
Events: {
52
52
endpoint:
53
- " https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-east-1.amazonaws.com/event" ,
54
- region: " us-east-1" ,
55
- defaultAuthMode: " apiKey" ,
56
- apiKey: " da2-abcdefghijklmnopqrstuvwxyz" ,
53
+ ' https://abcdefghijklmnopqrstuvwxyz.appsync-api.us-east-1.amazonaws.com/event' ,
54
+ region: ' us-east-1' ,
55
+ defaultAuthMode: ' apiKey' ,
56
+ apiKey: ' da2-abcdefghijklmnopqrstuvwxyz' ,
57
57
},
58
58
},
59
59
});
60
60
61
61
export default function App() {
62
62
const [myEvents, setMyEvents] = useState <unknown []>([]);
63
63
64
- const sub = useRef <ReturnType <EventsChannel [" subscribe" ]>>(null );
64
+ const sub = useRef <ReturnType <EventsChannel [' subscribe' ]>>(null );
65
65
66
66
useEffect (() => {
67
67
let channel: EventsChannel ;
68
68
69
69
const connectAndSubscribe = async () => {
70
- channel = await events .connect (" default/channel" );
70
+ channel = await events .connect (' default/channel' );
71
71
72
72
if (! sub .current ) {
73
73
sub .current = channel .subscribe ({
74
74
next : (data ) => {
75
- console .log (" received" , data );
75
+ console .log (' received' , data );
76
76
setMyEvents ((prev ) => [data , ... prev ]);
77
77
},
78
- error : (err ) => console .error (" error" , err ),
78
+ error : (err ) => console .error (' error' , err ),
79
79
});
80
80
}
81
81
};
@@ -91,11 +91,11 @@ export default function App() {
91
91
92
92
async function publishEvent() {
93
93
// Publish via HTTP POST
94
- await events .post (" default/channel" , { some: " data" });
94
+ await events .post (' default/channel' , { some: ' data' });
95
95
96
96
// Alternatively, publish events through the WebSocket channel
97
- const channel = await events .connect (" default/channel" );
98
- await channel .publish ({ some: " data" });
97
+ const channel = await events .connect (' default/channel' );
98
+ await channel .publish ({ some: ' data' });
99
99
}
100
100
101
101
return (
0 commit comments