diff --git a/packages/core/useWebSocket/index.md b/packages/core/useWebSocket/index.md index fd4d619c70a..0bd74b2c746 100644 --- a/packages/core/useWebSocket/index.md +++ b/packages/core/useWebSocket/index.md @@ -11,11 +11,17 @@ Reactive [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/ ```js import { useWebSocket } from '@vueuse/core' -const { status, data, send, close } = useWebSocket('ws://websocketurl') +const { status, data, send, open, close } = useWebSocket('ws://websocketurl') ``` See the [Type Declarations](#type-declarations) for more options. +### Immediate + +Auto connect (disabled by default, will be enabled by default in the future). + +This will call `open()` automatically for you and you don't need to call it by yourself. + ### Auto-reconnection Reconnect on errors automatically (disabled by default). @@ -119,6 +125,13 @@ export interface WebSocketOptions { */ onFailed?: Fn } + + /** + * Automatically open a connection + * + * @default false + */ + immediate?: boolean } export interface WebSocketResult { /** diff --git a/packages/core/useWebSocket/index.ts b/packages/core/useWebSocket/index.ts index 4172a3bfb91..c19b4ed26fb 100644 --- a/packages/core/useWebSocket/index.ts +++ b/packages/core/useWebSocket/index.ts @@ -55,6 +55,13 @@ export interface WebSocketOptions { */ onFailed?: Fn } + + /** + * Automatically open a connection + * + * @default false + */ + immediate?: boolean } export interface WebSocketResult { @@ -212,6 +219,8 @@ export function useWebSocket( heartbeatResume = resume } + if (options.immediate) _init() + const open = () => { close() retried = 0