Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Vue 3 composition-API support #519

@e-tobi

Description

@e-tobi

It would be nice to have a useSocket() to get the $socket within setup(). The basic steps would be:

  • declare a TS interface with $subscribe, $unsubscribe etc
  • declare an injection symbol and make it available to TS declared as InjectionKey<interface-defined-above>
  • declare a function useSocket() (TS return type is interface defined above) - implementation calls inject(symbol-defined-above)
  • in install do app.provide(symbol-defined-above, $socket)

I tried to provide a PR, but I fail to get the new exports working, always get a warning export 'useSocket' was not found in 'vue-socket.io-extended' and I have no clue why - this is not my comfort zone.

This is what I added to index.d.ts:

export interface SocketIO {
  client: SocketIOClient.Socket;
  $subscribe: (event: string, fn: Function) => void;
  $unsubscribe: (event: string) => void;
  connected: boolean;
  disconnected: boolean;
}

export declare const SocketKey: InjectionKey<SocketIO>;

export declare function useSocket(): SocketIO;

And plugin.js just has (provided in the export):

const SocketKey = Symbol('$socket');

function useSocket() {
  return inject(SocketKey);
}

And app.provide(SocketKey, $socket); was added to the install function.

May workaround for now is to do this in main.ts of the application using vue-socket.io-extended.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions