Skip to content

Commit 81babd2

Browse files
authored
feat: Vue 3 support
BREAKING CHANGE: migration to vue 3 (vue 2 is not s Merge pull request #507
2 parents aa5ce85 + aab3f24 commit 81babd2

13 files changed

+244
-345
lines changed

README.md

+16-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<a href="https://www.npmjs.com/package/vue-socket.io-extended"><img src="https://badgen.net/npm/v/vue-socket.io-extended" alt="Version"></a>
66
<a href="https://www.npmjs.com/package/vue-socket.io-extended"><img src="https://badgen.net/npm/dt/vue-socket.io-extended" alt="Downloads"></a>
77
<a href="https://www.npmjs.com/package/vue-socket.io-extended"><img src="https://badgen.net/npm/license/vue-socket.io-extended" alt="License"></a>
8-
<a href="https://vuejs.org/"><img src="https://badgen.net/badge/Vue/2.x/orange" alt="Vue.js 2.x compatible"></a>
8+
<a href="https://vuejs.org/"><img src="https://badgen.net/badge/Vue/3.x/orange" alt="Vue.js 3.x compatible"></a>
99
<a href="https://raw.githubusercontent.com/probil/vue-socket.io-extended/master/dist/vue-socket.io-ext.min.js"><img src="https://badgen.net/bundlephobia/min/vue-socket.io-extended" alt="Minified library size"></a>
1010
<a href="https://codecov.io/gh/probil/vue-socket.io-extended"><img src="https://badgen.net/codecov/c/github/probil/vue-socket.io-extended/master" alt="Code coverage (codecov)"></a>
1111
<a href="https://gitter.im/vue-socket-io-extended/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link"><img src="https://badgen.net/badge/chat/on%20gitter/cyan" alt="Join us Gitter"></a>
@@ -70,10 +70,12 @@ npm install vue-socket.io-extended socket.io-client
7070
```js
7171
import VueSocketIOExt from 'vue-socket.io-extended';
7272
import { io } from 'socket.io-client';
73+
import { createApp } from 'vue';
7374

7475
const socket = io('http://socketserver.com:1923');
7576

76-
Vue.use(VueSocketIOExt, socket);
77+
const app = createApp({});
78+
app.use(VueSocketIOExt, socket);
7779
```
7880
*Note:* you have to pass instance of `socket.io-client` as second argument to prevent library duplication. Read more [here](https://github.com/probil/vue-socket.io-extended/issues/19).
7981

@@ -85,7 +87,8 @@ Vue.use(VueSocketIOExt, socket);
8587
<script src="https://cdn.jsdelivr.net/npm/vue-socket.io-extended"></script>
8688
<script>
8789
var socket = io('http://socketserver.com:1923');
88-
Vue.use(VueSocketIOExt, socket);
90+
var app = createApp({});
91+
app.use(VueSocketIOExt, socket);
8992
</script>
9093
```
9194

@@ -96,7 +99,7 @@ Vue.use(VueSocketIOExt, socket);
9699
Define your listeners under `sockets` section and they will listen corresponding `socket.io` events automatically.
97100

98101
```js
99-
new Vue({
102+
createApp({
100103
sockets: {
101104
connect() {
102105
console.log('socket connected')
@@ -163,8 +166,9 @@ import { io } from 'socket.io-client';
163166
import store from './store'
164167

165168
const socket = io('http://socketserver.com:1923');
169+
const app = createApp({});
166170

167-
Vue.use(VueSocketIOExt, socket, { store });
171+
app.use(VueSocketIOExt, socket, { store });
168172
```
169173

170174
#### Receiving Events
@@ -188,15 +192,12 @@ Check the [Migration from VueSocketIO](#information_source-migration-from-vuesoc
188192
```js
189193
// In this example we have a socket.io server that sends message ID when it arrives
190194
// so to get entire body of the message we need to make AJAX call the server
191-
import Vue from 'vue'
192-
import Vuex from 'vuex'
195+
import { createStore } from 'vuex';
193196

194197
// `MessagesAPI.downloadMessageById` is an async function (goes to backend through REST Api and fetches all message data)
195198
import MessagesAPI from './api/message'
196199

197-
Vue.use(Vuex);
198-
199-
export default new Vuex.Store({
200+
export default createStore({
200201
state: {
201202
// we store messages as a dictionary for easier access and interaction
202203
// @see https://hackernoon.com/shape-your-redux-store-like-your-database-98faa4754fd5
@@ -237,10 +238,7 @@ Events can be sent to the Socket.IO server by calling `this._vm.$socket.client.e
237238
Namespaced modules are supported out-of-the-box. Any appropriately-named mutation or action should work regardless of whether it's in a module or in the main Vuex store.
238239

239240
```js
240-
import Vue from 'vue'
241-
import Vuex from 'vuex'
242-
243-
Vue.use(Vuex);
241+
import { createStore } from 'vuex';
244242

245243
const messages = {
246244
state: {
@@ -269,7 +267,7 @@ const notifications = {
269267
},
270268
};
271269

272-
export default new Vuex.Store({
270+
export default createStore({
273271
modules: {
274272
messages,
275273
notifications,
@@ -296,11 +294,10 @@ Check the example below:
296294
```vue
297295
<!-- App.vue -->
298296
<script>
299-
import Vue from 'vue'
300-
import Component from 'vue-class-component'
297+
import { Options, Vue } from 'vue-class-component';
301298
import { Socket } from 'vue-socket.io-extended'
302299
303-
@Component({})
300+
@Options({})
304301
export default class App extends Vue {
305302
@Socket() // --> listens to the event by method name, e.g. `connect`
306303
connect () {
@@ -416,7 +413,7 @@ const ioInstance = io('https://hostname/path', {
416413
reconnectionDelay: 500,
417414
maxReconnectionAttempts: Infinity
418415
});
419-
Vue.use(VueSocketIO, ioInstance, {
416+
app.use(VueSocketIO, ioInstance, {
420417
store, // vuex store instance
421418
actionPrefix: 'SOCKET_', // keep prefix in uppercase
422419
eventToActionTransformer: (actionName) => actionName // cancel camel case

0 commit comments

Comments
 (0)