-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2893 from Hyperkid123/ws-subs
Expose WS subscription chrome API.
- Loading branch information
Showing
7 changed files
with
115 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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,49 @@ | ||
# Websocket subscription API | ||
|
||
> This API is experimental and is restricted only to the notification drawer and other internal chrome APIs. If you are interested in using the WS API, contact the platform experience services team. | ||
## Subscribing to an event | ||
|
||
To consume events, the following information is necessary | ||
- the event type | ||
- the event payload shape | ||
|
||
Once this information is know, you can subscribe using the chrome API: | ||
|
||
```tsx | ||
import useChrome from '@redhat-cloud-services/frontend-components/useChrome'; | ||
import { ChromeWsPayload, ChromeWsEventTypes } from '@redhat-cloud-services/types'; | ||
// depends on the event type | ||
type EventPayload = { | ||
description: string; | ||
id: string; | ||
read: boolean; | ||
title: string; | ||
}; | ||
|
||
const eventType: ChromeWsEventTypes = 'foo.bar'; | ||
|
||
const ConsumerComponent = () => { | ||
const { addWsEventListener } = useChrome(); | ||
const [data, setData] = useState<ChromeWsPayload<EventPayload>[]>([]) | ||
|
||
function handleWsEvent(event: ChromeWsPayload<EventPayload>) { | ||
// handle the event according to requirements | ||
setData(prev => [...prev, event]) | ||
} | ||
|
||
useEffect(() => { | ||
const unRegister = addWsEventListener(eventType, handleWsEvent) | ||
return () => { | ||
// Do not forget to clean the listener once the component is removed from VDOM | ||
unRegister() | ||
} | ||
}, []) | ||
|
||
return ( | ||
// somehow use the data | ||
) | ||
} | ||
|
||
|
||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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