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

@dreamonkey/vue-signalr

v1.2.0

Published

SignalR plugin for Vue 3

Downloads

3,072

Readme

vue-signalr

A Vue3 plugin which wraps SignalR and provider stricter typings.

Installation

Install this package and SignalR peer dependency

$ yarn add @dreamonkey/vue-signalr @microsoft/signalr

Apply the plugin providing a HubConnection instance

import { VueSignalR } from "@dreamonkey/vue-signalr";
import { HubConnectionBuilder } from "@microsoft/signalr";
import { createApp } from "vue";
import App from "./App.vue";

// Create your connection
// See https://docs.microsoft.com/en-us/javascript/api/@microsoft/signalr/hubconnectionbuilder
const connection = new HubConnectionBuilder()
  .withUrl("http://localhost:5000/signalr")
  .build();

createApp(App).use(VueSignalR, { connection }).mount("#app");

Usage

import { useSignalR } from "@dreamonkey/vue-signalr";
import { inject } from "vue";

export default {
  setup() {
    const signalr = useSignalR();

    signalr.invoke("SendMessage", { message });

    signalr.on("MessageReceived", ({ message }) => {
      /* do stuff */
    });
  },
};

While SignalR doesn't make a distinction between client side and server side methods, into this package we refer the formers as "Events" and the latters as "Commands".

Commands can be sent using signalr.invoke(), while events can be enabled or disabled using signalr.on() and signalr.off()

Unsubscribing

All Event you create a listener for using signalr.on() must be unsubscribed when not used anymore, to avoid memory leaks and erratic code behavior. If you call signalr.on() within a Vue component setup scope, the listener will be unsubscribed automatically into onBeforeUnmount hook. This behavior can be disabled via autoOffInsideComponentScope plugin option.

If you disabled it, or you start a listener outside a component scope, you'll need to unsubscribe it manually using signal.off() and providing the same listener function reference to it. Not providing it will remove all listeners from the provided Event

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

signalr.on("MessageReceived", messageReceivedCallback);

signalr.off("MessageReceived", messageReceivedCallback); // Remove this listener
signalr.off("MessageReceived"); // Remove all listeners on `MessageReceived` event

Type-safety

Command and Events names and payloads are registered via type augmentation of dedicated interfaces (SignalREvents and SignalRCommands) into @dreamonkey/vue-signalr scope. These types are later used to provide you autocompletion.

The package works with plain strings too, you're not required to register Commands/Events typings

import "@dreamonkey/vue-signalr";

interface MessageReceivedPayload {
  message: string;
}

interface SendMessagePayload {
  message: string;
}

declare module "@dreamonkey/vue-signalr" {
  interface SignalREvents {
    // Define an event, its payload is a single parameter of type `MessageReceivedPayload`
    MessageReceived: MessageReceivedPayload;
    // Define an event, its payload is composed by 0 or more parameters of type `MessageReceivedPayload`
    MultipleMessagesReceived: MessageReceivedPayload[];
    // Define an event, its payload is composed by exactly 2 parameters of type `MessageReceivedPayload`
    TwoMessagesReceived: [MessageReceivedPayload, MessageReceivedPayload];
    // Define an event with no payload
    MainTopicJoined: false;
  }

  interface SignalRCommands {
    // Define a command and its payload
    SendMessage: SendMessagePayload;
    // Define a command with no payload
    JoinMainTopic: false;
  }
}

Methods remapping

In case you need to remap a Command or Event name, you could do so using remapMethod or remapMethods helpers

import { remapMethod } from "@dreamonkey/vue-signalr";

remapMethod("receiveMessageWithStrangeMethodName", "MessageReceived");

remapMethods([
  ["legacyTopic2Joined", "MainTopicJoined"],
  ["createNewMessage", "SendMessage"],
]);

Error Handling

You can react to connection/reconnection errors providing a failFn option into the plugin options

import { VueSignalR } from "@dreamonkey/vue-signalr";
import { createApp } from "vue";
import App from "./App.vue";

const connection = new HubConnectionBuilder()
  .withUrl("http://localhost:5000/signalr")
  .build();

createApp(App)
  .use(VueSignalR, {
    connection,
    failFn: () => {
      /* do stuff */
    },
  })
  .mount("#app");

Possible next steps

  • take inspiration from other helpers into https://socket.io/docs/v4/listening-to-events/
  • take inspiration from other features at the end of https://socket.io/docs/v4/how-it-works/
  • add rooms join/leave abstractions (?)

Credits

This package has been inspired by https://github.com/quangdaon/vue-signalr