Giter Site home page Giter Site logo

fork-vue3-websocket's Introduction

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, {
    secured: false,
    host: 'localhost:9000',
    protocols: ['soap']
}) */

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, inject } from 'vue'
import { onMessage, onOpen, onClose, onError } from 'vue3-websocket'

const text = ref('')

const socket = inject('socket')

const sendMessage = () => socket.value.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?: boolean,
    host: string,
    debug?: boolean,
    reconnect?: boolean,
    reconnectTime?: number,
    protocols?: string[]
}

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

Events:

  • onOpen
  • onMessage
  • onClose
  • onError

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.