22
33import * as Ably from 'ably' ;
44
5- import { AblyProvider , useChannel } from "ably/react"
5+ import { AblyProvider , ChannelProvider , useChannel } from "ably/react"
66import { useState , useEffect } from 'react'
77import Logger , { LogEntry } from '../../components/logger' ;
88import SampleHeader from '../../components/SampleHeader' ;
99
1010export default function Presence ( ) {
1111
12- const client = new Ably . Realtime . Promise ( { authUrl :'/token' , authMethod : 'POST' } ) ;
12+ const client = new Ably . Realtime ( { authUrl :'/token' , authMethod : 'POST' } ) ;
1313
1414 return (
1515 < AblyProvider client = { client } >
16- < div className = "flex flex-row justify-center" >
17- < div className = "flex flex-col justify-start items-start gap-10 h-[172px]" >
18- < div className = "flex flex-col justify-start items-start gap-4" >
19- < SampleHeader sampleName = "History" sampleIcon = "History.svg" sampleDocsLink = "https://ably.com/docs/storage-history/history?lang=javascript" />
20- < div className = "font-manrope text-base max-w-screen-sm text-slate-800 text-opacity-100 leading-6 font-light" >
21- Retrieve a history of messages that have been published to a channel.
22- Messages are only stored for 2 minutes by default. In order for them
23- to be stored for longer you should enable the
24- < span className = "text-xs font-jetbrains-mono font-medium bg-slate-200 p-1" >
25- Persist all messages
26- </ span >
27- < a href = "https://ably.com/docs/channels?lang=javascript#rules" target = "blank" > < span className = "text-sky-600 text-opacity-100" > channel rule</ span > </ a >
28- < span className = "text-black text-opacity-100" > </ span > for
29- the
30- < span className = "text-xs font-jetbrains-mono font-medium bg-slate-200 p-1" >
31- status-updates
32- </ span >
33- channel in your Ably app
16+ < ChannelProvider channelName = "status-updates" >
17+ < div className = "flex flex-row justify-center" >
18+ < div className = "flex flex-col justify-start items-start gap-10 h-[172px]" >
19+ < div className = "flex flex-col justify-start items-start gap-4" >
20+ < SampleHeader sampleName = "History" sampleIcon = "History.svg" sampleDocsLink = "https://ably.com/docs/storage-history/history?lang=javascript" />
21+ < div className = "font-manrope text-base max-w-screen-sm text-slate-800 text-opacity-100 leading-6 font-light" >
22+ Retrieve a history of messages that have been published to a channel.
23+ Messages are only stored for 2 minutes by default. In order for them
24+ to be stored for longer you should enable the
25+ < span className = "text-xs font-jetbrains-mono font-medium bg-slate-200 p-1" >
26+ Persist all messages
27+ </ span >
28+ < a href = "https://ably.com/docs/channels?lang=javascript#rules" target = "blank" > < span className = "text-sky-600 text-opacity-100" > channel rule</ span > </ a >
29+ < span className = "text-black text-opacity-100" > </ span > for
30+ the
31+ < span className = "text-xs font-jetbrains-mono font-medium bg-slate-200 p-1" >
32+ status-updates
33+ </ span >
34+ channel in your Ably app
35+ </ div >
3436 </ div >
35- </ div >
36- < div className = "flex flex-row justify-start items-start gap-4 pt-4 pr-4 pb-4 pl-4 rounded-lg border-slate-200 border-t border-b border-l border-r border-solid border w-[480px] h-[72px] bg-white" >
37- < div className = "flex flex-col justify-center items-center h-5" >
38- < img width = "24px" height = "22px" src = "/assets/Alert.svg" alt = "Alert" />
39- </ div >
40- < div className = "font-manrope text-sm text-gray-500 text-opacity-100 leading-5 font-light" >
41- < span className = "font-medium" > Important: </ span > You need to
42- publish at least 1 message from the
43- < a href = "/pub-sub" target = "_blank" > < span className = "text-sky-600 text-opacity-100" >
44- Pub/Sub Channels example
45- </ span > </ a >
46- to see history log.
37+ < div className = "flex flex-row justify-start items-start gap-4 pt-4 pr-4 pb-4 pl-4 rounded-lg border-slate-200 border-t border-b border-l border-r border-solid border w-[480px] h-[72px] bg-white" >
38+ < div className = "flex flex-col justify-center items-center h-5" >
39+ < img width = "24px" height = "22px" src = "/assets/Alert.svg" alt = "Alert" />
40+ </ div >
41+ < div className = "font-manrope text-sm text-gray-500 text-opacity-100 leading-5 font-light" >
42+ < span className = "font-medium" > Important: </ span > You need to
43+ publish at least 1 message from the
44+ < a href = "/pub-sub" target = "_blank" > < span className = "text-sky-600 text-opacity-100" >
45+ Pub/Sub Channels example
46+ </ span > </ a >
47+ to see history log.
48+ </ div >
49+
4750 </ div >
48-
51+ < HistoryMessages />
4952 </ div >
50- < HistoryMessages />
5153 </ div >
52- </ div >
54+ </ ChannelProvider >
5355 </ AblyProvider >
5456 )
5557}
@@ -59,19 +61,19 @@ function HistoryMessages() {
5961 const [ realtimeLogs , setRealtimeLogs ] = useState < Array < LogEntry > > ( [ ] )
6062 const [ historicalLogs , setHistoricalLogs ] = useState < Array < LogEntry > > ( [ ] )
6163
62- const { channel } = useChannel ( "status-updates" , ( message : Ably . Types . Message ) => {
64+ const { channel } = useChannel ( "status-updates" , ( message : Ably . Message ) => {
6365 console . log ( message ) ;
6466 setRealtimeLogs ( prev => [ ...prev , new LogEntry ( `✉️ event name: ${ message . name } text: ${ message . data . text } ` ) ] )
6567 } ) ;
6668
6769 useEffect ( ( ) => {
6870 const getHistory = async ( ) => {
69- let history : Ably . Types . PaginatedResult < Ably . Types . Message > = await channel . history ( )
71+ let history : Ably . PaginatedResult < Ably . Message > | null = await channel . history ( )
7072 do {
7173 history . items . forEach ( message => {
7274 setHistoricalLogs ( prev => [
7375 ...prev ,
74- new LogEntry ( `"${ message . data . text } " sent at ${ new Date ( message . timestamp ) . toISOString ( ) } ` )
76+ new LogEntry ( `"${ message . data . text } " sent at ${ new Date ( message . timestamp ! ) . toISOString ( ) } ` )
7577 ] )
7678 } )
7779 history = await history . next ( )
0 commit comments