Giter Site home page Giter Site logo

vue-signalr's Introduction

Vue SignalR Plugin

This plugin has only been tested with Vue 3 and TypeScript. Use at your own risk.

Installation

To install in Vue 3:

import { createApp } from 'vue';
import { VueSignalR } from '@quangdao/vue-signalr';
import App from './App.vue';

createApp(App)
  .use(VueSignalR, { url: 'http://localhost:5000/signalr' })
  .mount('#app');

Usage

To use signalr, you need to inject the service like so:

import { inject } from 'vue';
import { SignalRSymbol } from '@quangdao/vue-signalr';

export default {
  setup() {
    const signalr = inject(SignalRSymbol);

    if (!signalr) throw new Error('Failed to inject SignalR');
  }
};

Events

For type safety, I recommend declaring constant methods like so:

import {
  SignalRServerMethod,
  SignalRClientMethod
} from '@quangdao/vue-signalr';

const SendMessage: SignalRServerMethod<MyObject> = 'SendMessage';
const MessageReceived: SignalRClientMethod<MyObject> = 'MessageReceived';

interface MyObject {
  prop: string;
}

Receiving Messages

If you used typed method keys mentioned above:

setup() {
  signalr.on(MessageReceived, (message) => console.log(message.prop));
}

Alternative, you can pass in the method name as a string, with an optional type argument for the data:

// Both of these work:
signalr.on('MessageReceived', message => console.log(message.prop)); // Data object is untyped
signalr.on<MyObject>('MessageReceived', message => console.log(message.prop));

Unsubscribing

Eventually, I want to automatically unbind all subscription within the context of a component when that component is destroyed, but for now I recommend unsubscribing from all of your connections onBeforeUnmount.

Same rules regarding method keys above apply here.

setup() {
  const messageReceivedCallback = (message) => console.log(message.prop);

  signalr.on(MessageReceived, messageReceivedCallback);
  onBeforeUnmount(() => signalr.off(MessageReceived, messageReceivedCallback));
}

Sending Message

Same rules regarding method keys above apply here.

signalr.invoke(SendMessage, { prop: 'value' });

Error Handling

Errors are handled at the app level by passing in a property to the options object. I used a redirect here, but you can probably get creative with some fancy state management or something.

import { createApp } from 'vue';
import { VueSignalR } from '@quangdao/vue-signalr';
import router from './router';
import App from './App.vue';

createApp(App)
  .use(VueSignalR, {
    url: 'http://localhost:5000/signalr',
    disconnected() {
      router.push('/disconnected');
    }
  })
  .mount('#app');

vue-signalr's People

Contributors

quangdaon avatar

Watchers

James Cloos avatar

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.