npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-vue-websocket

v2.0.2

Published

Simple client for communicate VueJS and AdonisJS throught websocket

Downloads

211

Readme

Adonis websocket client for VueJS

How it works

This package add new layer for use original AdonisWSClient in your vue.js project:

//For global acceess
Vue.ws

//In vue component
this.$ws

For example you can handle adonisWS events in your vue components:

this.$ws.$on(`${topicName}|ABOUT_MESSAGE`, d => console.log(d)) 

Emit events from Vue components you can:

//if this topic does not exist, $emitToServer automatic trying to create it
this.$ws.$emitToServer(topicName, 'hello', {message: 'message})

Install

npm

npm install adonis-vue-websocket

yarn

yarn add adonis-vue-websocket

Usage

1. Connect plugin

You can to plug original AdonisWSClient and connect with library:

import Ws from '@adonisjs/websocket-client'
import WsPlugin from 'adonis-vue-websocket'
Vue.use(WsPlugin, { adonisWS: Ws })

Or

I recommend this way: add AdonisWS in index.html (for disable native AdonisJS logs):

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.4.3/polyfill.min.js"></script>
<script src="https://unpkg.com/@adonisjs/[email protected]/dist/Ws.browser.min.js"></script>

and connect with library in main.js:

import WsPlugin from 'adonis-vue-websocket'
Vue.use(WsPlugin, { adonisWS: window.adonis.Ws })

2. Init connection

Create file src/WsSubscriptions.js with content:

import Vue from "vue";

const userTopicSubscriptions = id => {
  if (id) {
    let subscription = Vue.ws.socket.getSubscription(`user:${id}`);
    if (!subscription) {
      subscription = Vue.ws.subscribe(`user:${id}`);
    }
    subscription.on("HELLO_EVENT", data => {
      console.log('Hello (event handled in src/WsSubscriptions.js)', data)
    });
  }
};

export default async () => {
  return new Promise((resolve, reject) => {
    Vue.ws.disconnect()
    Vue.ws.connect(
        {
            wsDomain: "ws://localhost:3333", 
            jwtToken: null
        }, 
        { 
            path: 'adonis-ws', 
            reconnectionAttempts: 300, 
            reconnectionDelay: 5000 
        }
    );
    Vue.ws.socket.on("open", () => {
      userTopicSubscriptions(1);
      resolve()
    });
    
    // FOR EXAMPLE you can observe for userId or another variable from Vuex
    // store.watch(
    //   () => store.getters.vgUserUid,
    //   async id => {
    //     if (id) {
    //       userTopicSubscriptions(uid);
    //     }
    //   }
    // );
  })
};

In app.vue:

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>
    </div>
    <router-view v-if="!loading"/>
  </div>
</template>

<script>
import WsSubscriptions from './WsSubscriptions'
export default {
  data () {
    return {
      loading: true
    }
  },
  async created() {
    await WsSubscriptions()
    this.loading = false
  }
}
</script>

3. Handle and emit events

You can handle events in any .js and .vue files and components, for example in vue component:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <el-input v-model="message"/>
    <el-button @click="sendHello">Send!</el-button>
  </div>
</template>
<script>
const topicName = 'user:1'
export default {
  data() {
    return {
      message: 'Hello from about page!'
    }
  },
  mounted() {
    this.$ws.$on(`${topicName}|ABOUT_MESSAGE`, this.handleAboutMessageEvent) 
    this.$ws.$on('ABOUT_MESSAGE', this.handleAboutMessageEvent)
  },
  beforeDestroy(){
    //Remove listeners when component destroy
    this.$ws.$off(`${topicName}|ABOUT_MESSAGE`, this.handleAboutMessageEvent);
    this.$ws.$off('ABOUT_MESSAGE', this.handleAboutMessageEvent);
  },
  methods: {
    handleAboutMessageEvent(data){
        console.log("handled in src/views/About.vue", data)
    },
    sendHello(){
      this.$ws.$emitToServer(topicName, 'hello', {message: this.message})
    }
  }
}
</script>

Profit!


Test

git clone https://github.com/reg2005/adonis-vue-websocket.git
cd adonis-vue-websocket

In first terminal

cd example/back
cp .env.example .env
npm install
adonis serve --dev

In second terminal

cd example/front 
npm install
yarn serve

And open front in your browser