-
Notifications
You must be signed in to change notification settings - Fork 79
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
[Amplify Gen2] How to Set up a real-time event subscription?? #2693
Comments
I tried to inject that subscription part to 'use client'
import { createContext, FC, ReactNode, useLayoutEffect } from 'react'
import { userClient } from '@/utils/generateClient/userClient'
import { Hub } from 'aws-amplify/utils'
import { CONNECTION_STATE_CHANGE, ConnectionState } from 'aws-amplify/api'
interface OrderEventProps {
}
const OrderEvent = createContext<OrderEventProps | undefined>(undefined)
interface OrderEventListenerProviderProps {
children: ReactNode
}
export const OrderEventListenerProvider: FC<OrderEventListenerProviderProps> = ({ children }) => {
useLayoutEffect(() => {
// @ts-ignore FIXME: Expression produces a union type that is too complex to represent.
const orderEventUnsubscribe = userClient.models.Order.onUpdate().subscribe({
next: (data) => console.log(`Order updated: ${JSON.stringify(data, null, 2)}`),
error: (error) => console.warn(`Order update error: ${error}`)
})
return () => {
orderEventUnsubscribe.unsubscribe()
}
}, [])
useLayoutEffect(() => {
// @ts-ignore FIXME: Expression produces a union type that is too complex to represent.
const orderEventUnsubscribe = userClient.models.Order.onCreate().subscribe({
next: (data) => console.log(`Order created: ${JSON.stringify(data, null, 2)}`),
error: (error) => console.warn(`Order create error: ${error}`)
})
return () => {
orderEventUnsubscribe.unsubscribe()
}
}, [])
useLayoutEffect(() => {
const hubDataUnsubscribe = Hub.listen('api', (data: any) => {
const { payload } = data
console.log(`API hub data is ${payload}`)
if (payload.event === CONNECTION_STATE_CHANGE) {
const connectionState = payload.data.connectionState as ConnectionState
console.log(connectionState)
}
})
return () => {
hubDataUnsubscribe()
}
}, [])
return <OrderEvent.Provider value={undefined}>{children}</OrderEvent.Provider>
} |
I also did this trial, but have this error messge. import { generateClient } from 'aws-amplify/api'
import { Schema } from '@/amplify/data/resource'
export const userClient = generateClient<Schema>({ authMode: 'userPool' })
const createSub = userClient.models.Order.onCreate().subscribe({
next: (data) => console.log(`Order created: ${JSON.stringify(data, null, 2)}`),
error: (error) => console.warn(`Order create error: ${error}`)
})
const updateSub = userClient.models.Order.onUpdate().subscribe({
next: (data) => console.log(`Order updated: ${JSON.stringify(data, null, 2)}`),
error: (error) => console.warn(`Order update error: ${error}`)
})
|
Oh I re-location the declaration of subscription method like this and it well-works. Amplify.configure(config, { ssr: true })
export default function ConfigureAmplifyClientSide() {
useEffect(() => {
// @ts-ignore FIXME: Expression produces a union type that is too complex to represent.
const subOrderEvent = userClient.models.Order.onCreate().subscribe({
next: (data) => console.log(`Order created: ${JSON.stringify(data, null, 2)}`),
error: (error) => console.warn(`Order create error: ${JSON.stringify(error, null, 2)}`)
})
return () => {
subOrderEvent.unsubscribe()
}
}, [])
return null
} |
This issue is now closed. Comments on closed issues are hard for our team to see. |
Amplify CLI Version
1.1.0 (npx ampx --version)
Question
To set up a real-time event subscription, i referred to this document
And this is my code.
But, when i created a new order data with that
userClient
on theclient side
, aboveonCreate()
method orHub.listen
method was not remained any logs.How should I set up the real-time event subscription?
The text was updated successfully, but these errors were encountered: