diff --git a/README.md b/README.md index f81bf17..9732682 100644 --- a/README.md +++ b/README.md @@ -139,9 +139,9 @@ type UseWebSocket = ( } = {}, shouldConnect: boolean = true, ): { - sendMessage: (message: string) => void, + sendMessage: (message: string, keep = boolean = true) => void, //jsonMessage must be JSON-parsable - sendJsonMessage: (jsonMessage: any) => void, + sendJsonMessage: (jsonMessage: any, keep = boolean = true) => void, //null before first received message lastMessage: WebSocketEventMap['message'] | null, //null before first received message. If message.data is not JSON parsable, then this will be a static empty object @@ -184,13 +184,13 @@ const { ### sendMessage ```ts -type sendMessage = (message: string) => void; +type sendMessage = (message: string, keep: boolean = true) => void; ``` -The argument sent through sendMessage will be passed directly to WebSocket#`send`. `sendMessage` will be static, and thus can be passed down through children components without triggering prop changes. Messages sent before the WebSocket is open will be queued up and sent on connection. +The argument sent through sendMessage will be passed directly to WebSocket#`send`. `sendMessage` will be static, and thus can be passed down through children components without triggering prop changes. Messages sent before the WebSocket is open will be queued up and sent on connection. If you don't want to use messages queue for a particular message you should use a 'keep' parameter. ### sendJsonMessage ```ts -type sendJsonMessage = (message: any) => void; +type sendJsonMessage = (message: any, keep: boolean = true) => void; ``` Message will first be passed through `JSON.stringify`. @@ -383,4 +383,4 @@ If used, an [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/Event - Currently, the library will set the readyState to CLOSED on the underlying `EventSource`'s onerror callback, and will also trigger `Options#onClose`, if provided. In this case, reconnect logic is driven by `Options#retryOnError`, instead of `Options#shouldReconnect`. - There is no 'CLOSING' readyState for `EventSource`, and as such, the CLOSED readyState is `2` for an `EventSource`, whereas it is `3` for a WebSocket. For purposes of internal consistency, the `readyState` returned by `useWebSocket` will follow the `WebSocket` enumeration and use `3` for the CLOSED event for both instance types. - `getEventSource` will return the underlying EventSource, even if `Options#share` is used -- as opposed to the `WebSocket` equivalent which returns a `Proxy`. -- There is no concept of sending messages from the client, and as such `sendMessage` will not be provided. \ No newline at end of file +- There is no concept of sending messages from the client, and as such `sendMessage` will not be provided. diff --git a/src/lib/use-websocket.ts b/src/lib/use-websocket.ts index 19dc150..fc60ce2 100644 --- a/src/lib/use-websocket.ts +++ b/src/lib/use-websocket.ts @@ -49,7 +49,7 @@ export const useWebSocket = ( const stringifiedQueryParams = options.queryParams ? JSON.stringify(options.queryParams) : null; - const sendMessage: SendMessage = useCallback(message => { + const sendMessage: SendMessage = useCallback((message, keep = true) => { if (isEventSourceSupported && webSocketRef.current instanceof EventSource) { console.warn('Unable to send a message from an eventSource'); return; @@ -58,13 +58,13 @@ export const useWebSocket = ( if (webSocketRef.current && webSocketRef.current.readyState === ReadyState.OPEN) { assertIsWebSocket(webSocketRef.current); webSocketRef.current.send(message); - } else { + } else if (keep) { messageQueue.current.push(message); } }, []); - const sendJsonMessage: SendJsonMessage = useCallback(message => { - sendMessage(JSON.stringify(message)); + const sendJsonMessage: SendJsonMessage = useCallback((message, keep = true) => { + sendMessage(JSON.stringify(message), keep); }, [sendMessage]); const getWebSocket = useCallback(() => {