Registry / web-framework / vue-socket.io-extended

vue-socket.io-extended

JSON →
library4.2.0jsnpmunverified

Vue-Socket.io-Extended is a robust library that provides seamless integration of Socket.io with Vue.js applications, including comprehensive support for Vuex state management. The current stable version is `4.2.0`, which targets Vue 2.x. Development is active for `v5.0.0-alpha` releases, which introduce support for Vue 3.x. The project aims to offer a more stable, tested, and feature-rich alternative to its inspiration, Vue-Socket.io, focusing on aspects like reactive properties for connection status, automatic dispatching of Vuex actions/mutations based on socket events, and strong TypeScript support. Its release cadence shows periodic updates, with significant work being done for the next major Vue 3 compatible version. It differentiates itself with lightweight design, good TypeScript support (including decorators), and flexibility in handling Socket.io client versions.

npm install vue-socket.io-extended
INSTALL
IMPORT
SIG · VUE-SOCKET.IO-EXTE
V
vue-socket.io-extended
web-frameworkjavascriptv4.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

VueSocketIOExtended
import VueSocketIOExtended from 'vue-socket.io-extended';
const VueSocketIOExtended = require('vue-socket.io-extended');
This is the default export for the plugin itself, used with `Vue.use()`. CommonJS `require` is generally discouraged in modern Vue projects favoring ESM.
io
import { io } from 'socket.io-client';
import io from 'socket.io-client';
`io` is a named export from `socket.io-client` in current versions, not a default export.
Socket
import Socket from 'vue-socket.io-extended/decorator';
import { Socket } from 'vue-socket.io-extended';
Since v5.0.0-alpha.3, the `@Socket()` decorator has a dedicated entrypoint. Previous versions might have exported it directly from the root. Requires `babel-plugin-transform-decorators-legacy` for Babel or `--experimentalDecorators` for TypeScript.

This quickstart initializes Vue-Socket.io-Extended with a Socket.io client, demonstrates listening for connection status and custom events, and shows how to emit events from a Vue component. It targets Vue 2.x, the stable version.

import Vue from 'vue'; import VueSocketIOExtended from 'vue-socket.io-extended'; import { io } from 'socket.io-client'; // Assuming a Socket.io server is running at http://localhost:3000 const socketInstance = io('http://localhost:3000'); // Optional: Import your Vuex store if you're using it // import store from './store'; Vue.use(VueSocketIOExtended, socketInstance, { // Pass your Vuex store here if needed // store: store, // Optional: Set custom event transformers for Vuex (e.g., to match custom naming conventions) // actionPrefix: 'SOCKET_', // Default // mutationPrefix: 'SOCKET_' }); new Vue({ el: '#app', data: () => ({ message: 'Waiting for messages...' }), sockets: { connect() { console.log('Socket connected successfully!'); this.message = 'Connected to server!'; }, disconnect() { console.log('Socket disconnected.'); this.message = 'Disconnected from server.'; }, // Listen for a custom 'chatMessage' event from the server chatMessage(data: string) { console.log('Received message:', data); this.message = `Received: ${data}`; } }, methods: { // Emit a 'sendMessage' event to the server sendMessage(payload: string) { if (this.$socket.connected) { this.$socket.emit('sendMessage', payload); console.log('Sent message:', payload); } else { console.warn('Socket not connected, message not sent.'); } } }, mounted() { this.sendMessage('Hello from Vue.js!'); }, template: ` <div id="app"> <h1>Vue-Socket.io-Extended Example</h1> <p>{{ message }}</p> <button @click="sendMessage('Ping from client!')">Send Ping</button> <p>Connection Status: {{ $socket.connected ? '🟢 Connected' : '🔴 Disconnected' }}</p> </div> ` });
Debug
Known issues
breakingVersion 5.0.0-alpha.1 introduced breaking changes, specifically dropping support for Vue 2.x in favor of Vue 3.x. Projects using Vue 2 must remain on the 4.x branch.
fix
For Vue 2 applications, stick to `vue-socket.io-extended@4.x.x`. For Vue 3, migrate to `vue-socket.io-extended@5.x.x` and adjust code accordingly for Composition API or updated Option API patterns.
affects: >=5.0.0-alpha.1
breakingThe `@Socket()` decorator's import path changed in v5.x. It is no longer exported from the root package.
fix
Update imports from `import { Socket } from 'vue-socket.io-extended'` to `import Socket from 'vue-socket.io-extended/decorator'`.
affects: >=5.0.0-alpha.3
gotchaWhen using the `@Socket()` decorator, ensure your build setup supports ECMAScript stage 1 decorators. This typically requires `babel-plugin-transform-decorators-legacy` for Babel or enabling `--experimentalDecorators` in TypeScript.
fix
Configure your Babel or TypeScript compiler to properly transpile decorators. Consult the documentation for your specific project setup (e.g., Vue CLI, Vite).
affects: >=4.x
gotchaSocket.io server and client versions must be compatible. Major version mismatches (e.g., Socket.io client v3 with server v2) can lead to connection issues.
fix
Ensure your `socket.io-client` package version aligns with your `socket.io` server package version. Refer to Socket.io's migration guides for compatibility matrices between major versions.
affects: >=4.x
gotchaIf integrating with Vuex, mutations should be prefixed with `SOCKET_` and actions with `socket_` by default to be automatically dispatched on socket events. This behavior is configurable.
fix
Follow the default naming convention or provide custom `actionPrefix` and `mutationPrefix` options during plugin installation.
affects: >=4.x
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'emit') or this.$socket is undefined
The `vue-socket.io-extended` plugin was not correctly installed via `Vue.use()` or the `socket.io-client` instance was not provided.
fix
Ensure `Vue.use(VueSocketIOExtended, socketInstance)` is called before creating your Vue app. Also verify `socket.io-client` is installed and `io()` is correctly used to create the `socketInstance`.
Failed to compile / Module not found: Error: Can't resolve 'socket.io-client'
The `socket.io-client` package is a required dependency but is not installed in the project.
fix
Install the `socket.io-client` package: `npm install socket.io-client` or `yarn add socket.io-client`.
Error during WebSocket handshake: Unexpected response code: 400
This often indicates a CORS (Cross-Origin Resource Sharing) issue or a mismatch in the Socket.io server configuration (e.g., path, transports).
fix
On your Socket.io server, ensure CORS is correctly configured to allow requests from your Vue application's origin. For example, `const io = new Server(server, { cors: { origin: 'http://localhost:8080' } });`. Also, check the connection URL and any `path` options.
The Socket instance will no longer forward the events emitted by its Manager
This is a change in behavior in `socket.io` version 3.x, where the `Socket` instance no longer automatically re-emits events from its underlying `Manager` (e.g., `connect_error`).
fix
If you relied on `socket.on('connect_error')` or similar on the `Socket` instance, you might need to explicitly listen on the `Manager` instance (`socket.client.on('connect_error')`) or adapt your error handling strategy.
Upgrade
Version history
4.2.0latest on npm
Audit
Dependencies
socket.io-clientrequiredCore dependency for establishing and managing Socket.io connections.
vuerequiredPeer dependency as it's a Vue.js plugin.
vuexoptionalOptional dependency for integrating with Vuex stores to manage real-time state.
Agent activity
36 hits · last 30 days
node
32
OpenAI (training)
1
Resources