You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define your listeners under `sockets` section, and they will be executed on corresponding `socket.io` events automatically.
100
100
@@ -119,6 +119,39 @@ createApp({
119
119
120
120
**Note**: Don't use arrow functions for methods or listeners if you are going to emit `socket.io` events inside. You will end up with using incorrect `this`. More info about this [here](https://github.com/probil/vue-socket.io-extended/issues/61)
121
121
122
+
#### On Vue.js component using the Composition API
123
+
124
+
When using the `setup` option of the Composition API, `this` is not available. In order to use socket.io, two composables can be used. `useSocket()` gives you the same object as `this.$socket` would be. With `onSocketEvent(event, callback)` you can subscribe to a `socket.io` event. Subscription will happen before the component is mounted and the component will automatically unsubscribe right before it is unmounted.
console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
139
+
});
140
+
141
+
constclickButton= (val) => {
142
+
// socket.client is the `socket.io-client` instance
143
+
socket.client.emit('emit_method', val);
144
+
};
145
+
146
+
return {
147
+
clickButton
148
+
}
149
+
}
150
+
})
151
+
```
152
+
153
+
**Note**: Don't subscribe / unsubscribe from events (via `$subscribe()` / `$unsubscribe` provided by `useSocket()`) directly in `setup()`. Always do this from within a lifecycle hook like `onBeforeMount` or `onBeforeUnmount` or in a method that is called once the component is created. The `onSocketEvent` composable will do this automatically for you. The reason is, that the moment `setup()` is executed the component is not yet instantiated and `$subscribe` /`$unsubscribe` therefore can't bind to a component instance.
154
+
122
155
#### Dynamic socket event listeners (changed in v4)
0 commit comments