#Server sent events with remix

1 messages · Page 1 of 1 (latest)

wild sapphire
lunar spear
#

Hello @wild sapphire I tried this solution but unfortunately it doesn't work.😔

wild sapphire
#

Remix version?

lunar spear
#

version : 1.8.2

#

`const { notificationsData } = useLoaderData<typeof loader>()

const [notifications, setNotifications] = React.useState<Notification[]>(
    notificationsData as any,
)

React.useEffect(() => {
    const url = new URL('http://localhost:3000/graphql')
    url.searchParams.set(
        'query',
        `subscription NotificationCreated {
                notificationCreated {
                    id
                    content
                    createdAt
                    updatedAt
                }            }`,
    )

    const evtSource = new EventSource(url, { withCredentials: true })
    evtSource.onmessage = (event) => {
        const data = JSON.parse(event.data)
            .data as NotificationCreatedSubscription

        setNotifications((prev: any) => [data.notificationCreated, ...prev])
    }
    return () => evtSource.close()
}, [])`