Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand Down Expand Up @@ -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.
- There is no concept of sending messages from the client, and as such `sendMessage` will not be provided.
8 changes: 4 additions & 4 deletions src/lib/use-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() => {
Expand Down