Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useWebSocket): add immediate option #503

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/core/useWebSocket/index.md
Expand Up @@ -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).
Expand Down Expand Up @@ -119,6 +125,13 @@ export interface WebSocketOptions {
*/
onFailed?: Fn
}

/**
* Automatically open a connection
*
* @default false
*/
immediate?: boolean
}
export interface WebSocketResult<T> {
/**
Expand Down
9 changes: 9 additions & 0 deletions packages/core/useWebSocket/index.ts
Expand Up @@ -55,6 +55,13 @@ export interface WebSocketOptions {
*/
onFailed?: Fn
}

/**
* Automatically open a connection
*
* @default false
*/
immediate?: boolean
}

export interface WebSocketResult<T> {
Expand Down Expand Up @@ -212,6 +219,8 @@ export function useWebSocket<Data = any>(
heartbeatResume = resume
}

if (options.immediate) _init()

const open = () => {
close()
retried = 0
Expand Down