🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

vue3-websocket

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue3-websocket

Simple package for implementing WebSocket into your Vue 3 application

1.0.8
Source
npm
Version published
Weekly downloads
139
-34.74%
Maintainers
1
Weekly downloads
 
Created
Source

Vue 3 WebSocket

Simple package for implementing WebSocket into your Vue 3 application using Composition API

Install dependency via npm

npm i vue3-websocket

For connection you should provide WS/WSS address as a string line or an object data

import { createApp } from 'vue'
import App from './App.vue'
import socket from 'vue3-websocket'

const app = createApp(App)

app.use(socket, 'ws://localhost:9000')

/*  OR use object data: 
app.use(socket, {
    secure: false,
    host: 'localhost:9000'
}) */

app.mount('#app')

Then you can use it in your components

<template>
    <input v-model="text" />
    <button @click="sendMessage">Send a message</button>
</template>

<script setup>
import { ref } from 'vue'
import { onMessage, onOpen, onClose, onError, send } from 'vue3-websocket'

const text = ref('')

const sendMessage = () => send(text.value)

onOpen(() => {
    console.log('WS connection is stable! ~uWu~')
})

onMessage(message => {
    console.log('Got a message from the WS: ', message)
})

onClose(() => {
    console.log('No way, connection has been closed 😥')
})

onError(error => {
    console.error('Error: ', error)
})
</script>

You can also inject socket connection directly

const socket = inject('socket')

There is a reactive readyState field available. You can track it with watchers

const readyState = inject('readyState')

watch(() => readyState.value, value => {
    console.log('New value: ', value)
})

Connection options interface:

interface Data {
    secured?: string,
    host: string,
    debug?: boolean,
    reconnect?: boolean,
    reconnectTime?: number
};

If debug is set to true, there will be debug messages in the console about each WS event

Events:

  • onOpen
  • onMessage
  • onClose
  • onError

Keywords

vue3

FAQs

Package last updated on 12 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts