question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuxt composition API (stable state)

See original GitHub issue

in composition-api there is no this, instead in the setup function we can get the closest to this object from context.app javascript const { app: { $axios, $cookies, $nuxtSocket, store } } = context one of the issues is that $store has been renamed to store inside app, so to solve this we bind the $nuxtSocket like so:

onMounted(() => {
      const boundNuxtSocket = $nuxtSocket.bind({ ...app, $store: store })
      socket.value = boundNuxtSocket({
        // nuxt-socket-io opts:
        name: 'main',

        // socket.io-client opts:
        reconnection: true,
      })
    })

still after doing all that we get errors:

error from store

Looking into that it seems these are the lines in question:

teardown({ ctx, socket, useSocket }) {
    if (ctx.onComponentDestroy === undefined) {
      ctx.onComponentDestroy = ctx.$destroy
    }

    ctx.$on('closeSockets', function() {
      socket.removeAllListeners()
      socket.close()
    })

ctx is seemed to be this meaning our app when looking what calls teardown:

if (teardown) {
    register.teardown({
      ctx: this,
      socket,
      useSocket
    })
  }

and this is what’s inside of app or this for $nuxtSocket: app object

current workaround is to pass to the $nuxtSocket options: teardown: false but then we miss out on that and need to implement teardown functionality ourselves.

so I’m not sure who/what is supposed to add $on to this object but that doesn’t happen.

please help

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
richardeschlosscommented, Mar 31, 2021

Branch feat/composition-api has just been pushed with some basic fixes:

  • implementation for $on, and $emit were created with tiny-emitter/instance (tiny-emitter added as a dependency)
  • implementation for $destroy added (requires onUnmounted to be passed in now as an option to nuxtSocket)
  • namespace config assignResp work for RefImpl types. (i.e., ref()'s, reactive())
  • stubbed ctx.$set in the plugin, ctx.$watch in the example component

The working component is components/Composition1.vue

Be careful trying to make the socket instance itself reactive because of it’s circular JSON structure.

The main file changed was io/plugin.js. Let me know how far these changes get you.

0reactions
richardeschlosscommented, Apr 1, 2021

1.1.17 released with basic support. Usage will have the details, and a fully working example is in Composition1.vue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuxt Composition API
Vue 3 Composition API. Get all the Composition API features in Nuxt 2. ; SSR features. Additional helpers to interact with SSR. ;...
Read more >
Nuxt Composition API, updating 'state', not reflected on UI ...
I had a Nuxt.js application working with the options API. And with the new Nuxt3 coming out, I was trying to migrate things...
Read more >
Add example on how to use with @vue/composition-api #6517
Please note that the Composition API is a major feature of Vue v3 which is still in pre-alpha state. Its probably safe to...
Read more >
Exploring Nuxt 3 and Composition API - YouTube
Vue 3 Stable release is out and most libraries are moving to vue 3, Nuxt 3 is also based on Vue 3. With...
Read more >
Nuxt 3 Stable Launch – All the details from Nuxt Nation 2022
All the information on the launch of Nuxt 3 Stable from the largest online Nuxt event, Nuxt Nation. Direct from the creator, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found