@@ -44,7 +44,7 @@ export const onChatMessage = socketInstance.createListener<ChatMessageType>()({
4444Now, use it in your component to listen for new messages. The ` onEvent ` helper hook lets you react to incoming data and
4545update your component's state.
4646
47- ``` tsx live title="Listening to chat messages"
47+ ``` tsx title="Listening to chat messages"
4848import { useListener } from " @hyper-fetch/react" ;
4949import { onChatMessage } from " ./api/socket" ;
5050
@@ -87,17 +87,20 @@ cluttering your component's rendering logic.
8787
88881 . ** ` onEvent ` ** : Fires whenever a new event is received from the server.
89892 . ** ` onError ` ** : Fires when a connection or subscription error occurs.
90- 3 . ** ` onOpen ` ** : Fires when the socket connection is successfully opened.
91- 4 . ** ` onClose ` ** : Fires when the socket connection is closed.
92- 5 . ** ` onReconnecting ` ** : Fires when the socket is attempting to reconnect.
90+ 3 . ** ` onConnected ` ** : Fires when the socket connection is successfully opened.
91+ 4 . ** ` onDisconnected ` ** : Fires when the socket connection is closed.
92+ 5 . ** ` onConnecting ` ** : Fires when the socket is connecting.
93+ 6 . ** ` onReconnecting ` ** : Fires when the socket is attempting to reconnect.
94+ 7 . ** ` onReconnectingFailed ` ** : Fires when the socket fails to reconnect.
9395
94- ``` tsx live title="Using event handlers"
96+ ``` tsx title="Using event handlers"
9597import { useListener } from " @hyper-fetch/react" ;
9698import { onChatMessage } from " ./api/socket" ;
9799
98100function App() {
99101 const [messages, setMessages] = useState ([]);
100- const { onEvent, onError, onOpen, onClose, onReconnecting } = useListener (onChatMessage );
102+ const { onEvent, onError, onConnected, onDisconnected, onConnecting, onReconnecting, onReconnectingFailed } =
103+ useListener (onChatMessage );
101104
102105 onEvent (({ data : message }) => {
103106 toast ({ type: " success" , message: ` New message: ${message .text } ` });
@@ -108,16 +111,24 @@ function App() {
108111 toast ({ type: " error" , message: ` Connection error: ${error .message } ` });
109112 });
110113
111- onOpen (() => {
114+ onConnected (() => {
112115 toast ({ type: " info" , message: " Socket connection opened." });
113116 });
114117
115- onClose (() => {
118+ onDisconnected (() => {
116119 toast ({ type: " warning" , message: " Socket connection closed." });
117120 });
118121
119- onReconnecting ((attempt ) => {
120- toast ({ type: " warning" , message: ` Reconnecting... (attempt ${attempt }) ` });
122+ onConnecting (() => {
123+ toast ({ type: " info" , message: " Socket is connecting." });
124+ });
125+
126+ onReconnecting (({ attempts }) => {
127+ toast ({ type: " warning" , message: ` Reconnecting... (attempt ${attempts }) ` });
128+ });
129+
130+ onReconnectingFailed (({ attempts }) => {
131+ toast ({ type: " error" , message: ` Reconnecting failed after ${attempts } attempts. ` });
121132 });
122133
123134 return (
0 commit comments