Integrating WebSockets in Nuxt and Nitro

Поділитися
Вставка
  • Опубліковано 9 лип 2024
  • 🎉 Nitro 2.9 was released just before Vue.js Amsterdam and brings new features such as a database layer, a task API and also WebSocket support! But how can we integrate it in a Nuxt application? This video will teach you 👌
    Key points:
    🛠 How to set up WebSockets in Nitro and in Nuxt
    💡 Working from scratch to a functional application
    ⚠️ Hints to make sure you can use Nitro WebSockets
    Info: Nuxt CLI v3.11.1 was released. YOU DON'T NEED THE NIGHTLY BUILD ANYMORE.
    ---
    Links:
    🔗 Code - github.com/manniL/alexander-l...
    🔗 Nuxt CLI - github.com/nuxt/cli
    🔗 Nitro WebSocket Docs - nitro.unjs.io/guide/websocket
    🔗 VueUse useWebsocket - vueuse.org/core/usewebsocket
    🔗 CrossWS - github.com/unjs/crossws
    📺 Proxying your API - • The BEST way to proxy ...
    Don't forget to hit that "Subscribe" button, ring the notification bell and give a thumbs up!
    🌐 Connect further:
    Website: www.lichter.io
    Twitter/X: / thealexlichter
    Twitch: / thealexlichter
    Chapters:
    00:00 Intro
    00:50 Setup to enable WebSockets
    03:02 Check dependency versions
    04:00 Demo App Setup
    04:22 Integrating WebSockets on the server
    07:15 Using it on the Client
    12:05 Verifying in the browser
    13:35 Adding one more functionality
    17:05 Summary
  • Наука та технологія

КОМЕНТАРІ • 92

  • @TheAlexLichter
    @TheAlexLichter  3 місяці тому +40

    Who was looking forward to WebSockets? 🙌🙌🙌

    • @Indam1
      @Indam1 3 місяці тому +1

      Wow, I started to work with WebSockets on Nuxt 3-5 hours ago and only now could create a chat. Time to solidify knowledge😁
      Thank you for the great videos🔥

  • @s4ndeep1203
    @s4ndeep1203 3 місяці тому +19

    Suggestion: Would love to have a video by you on Nuxt Layers !

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +3

      Noted 👌🏻

    • @Deepakyadav-de3pi
      @Deepakyadav-de3pi 3 місяці тому

      @@TheAlexLichter 1 suggestion nuxt layers with each layer having own local modules

  • @ZiadMalik-fs7ps
    @ZiadMalik-fs7ps 3 місяці тому +9

    Super relevant video for an app I am working on right now, keep up the good videos! Not enough Nuxt content by far!

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +2

      Perfect! Glad it could help you straight away 🔥
      I’ll definitely keep it up with the content! Your Super Thanks is ensuring it can keep going on and I can invest more time into UA-cam 🙏🏻
      Thank you so much! 🙌🏻

  • @damiangowala5681
    @damiangowala5681 3 місяці тому +4

    An in-depth guide on Vite PWA for Nuxt would be amazing to see (workbox, caching, custom installation prompt, etc.); currently, there are very few resources on it

  • @lbmgary
    @lbmgary 29 днів тому +1

    This is so awesome. Coming from Laravel where they do sooooo much to try to make it easy and, sorry, it just isn't. What a great demo. Rocking a connection with home assistant now turning lights on and off. Going to make it pretty and share it :)

    • @TheAlexLichter
      @TheAlexLichter  20 днів тому

      Yes please! Definitely share it! Would love to see more examples on it 🔥

  • @todpale
    @todpale 3 місяці тому +1

    We finally have it in Nuxt! Almost, but still! Thanks for the video!

  • @randomtimessomehow
    @randomtimessomehow 3 місяці тому +2

    I love these videos, I'm learning something new everytime :) Keep up the good work

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      Thank you so much Phillip! I highly appreciate it 🙏🏻
      Donations like yours will allow me to spend even more time on the channel and such videos 🔥

  • @prishanf
    @prishanf Місяць тому

    Very easy to understand example. I’m loving Nuxt how simple the web socket api is both FE and backend.

    • @TheAlexLichter
      @TheAlexLichter  Місяць тому

      Delighted to hear that the example was great ☺️
      Right? The APIs are so elegant 🔥

  • @luc122c
    @luc122c 3 місяці тому

    Thanks for another great video! I’m going to have a look at proxying web sockets using nitro

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      You are welcome 🙌🏻
      Were you successful? ☺️

    • @luc122c
      @luc122c 3 місяці тому

      @@TheAlexLichter I still have some stuff to workout, it's not going as I expected but that's the fun of living on the edge 😝

  • @valacshiro378
    @valacshiro378 3 місяці тому

    Thank you, this is amazing I love your vids they are always helpful

  • @nanaassumadu4997
    @nanaassumadu4997 3 місяці тому

    Thank you...This is awesome... I will definitely implement this in all my Nuxt project as a way to get feedback from my users
    Thanks a lot

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      You are welcome! How did the implementation go?

  • @unseriousdev
    @unseriousdev 3 місяці тому

    I already use this + the task feature 🔥
    Love you all 💚💚

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      So amazing, right?
      What do you use it for? ☺️

    • @unseriousdev
      @unseriousdev 3 місяці тому

      @@TheAlexLichter I have a task that runs every 48hrs to purge unverified accounts for a community website I'm building.
      The websocket feature to show some kind of progress to the users (some applications have multi stages).
      Love and good wishes to you all, especially Pooya💚

  • @michaelpeters9551
    @michaelpeters9551 28 днів тому

    You are awesome thank you!

  • @m.h.323
    @m.h.323 2 місяці тому +1

    Still can't wrap my head around the functionality. Tested the demo, which works fine, but that requires everything is in one composable. How would I approach it when I have an existing service, that is listening to events and writes in the DB. I want to get that eventName and push it to the Websocket. Example would be great.

  • @productdevbook
    @productdevbook 3 місяці тому +1

    Thank you ❤

  • @SKTTWkartrider
    @SKTTWkartrider 3 місяці тому +1

    Rare video that Alex filmed during day

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      Well spotted! I did today as I was busy writing my thesis until Wednesday 👀😁

  • @devoxygen7395
    @devoxygen7395 3 місяці тому

    Lub dude

  • @iUmerFarooq
    @iUmerFarooq 3 місяці тому

    That's cool 🔥

  • @randomtimessomehow
    @randomtimessomehow 3 місяці тому

    I love these videos, I'm learning something new everytime :) I have a general question... Let's say I would actually have to build a chat functionality in my web app (luckily I don't!) in combination with Supabase. Supabase has a subscribe feature (I'm not sure if you're familiar with it). Could I develop such chat functionality solely with Supabase subscribe feature (so I can automatically retrieve new messages in realtime) or would I need the web socket handler on top of that?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      Glad to hear that the videos are helpful 🙏🏻
      Regarding your question: Yes, you could implement that with Supabase only as they provide the logic for broadcasting out of the box 👍🏻

    • @randomtimessomehow
      @randomtimessomehow 3 місяці тому

      Thanks again! 🙏🏻@@TheAlexLichter

  • @joschua6523
    @joschua6523 3 місяці тому

    Would love to see a video on IndexedDB and how to handle reactivity while using it!

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      I'd go with idb-keyval and the composable around @ vueuse.org/integrations/useIDBKeyval/ ☺️

  • @svenvanreenen
    @svenvanreenen 3 місяці тому

    This is really cool. I dont have much experience with websockets, so I have a storage question. How long will messages be stored, as long as the server runs? or should they be saved in a DB?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      As mentioned in a comment earlier - the messages must be saved externally to be persisted. WebSockets are only a way to communicate and do not persist data by default 😊

  • @silvesterwali6565
    @silvesterwali6565 3 місяці тому +1

    amazing .. video. can you make another video how to handle properly file uploaded in nuxt fullstack. like laravel they can upload assets for public and private. and those asset will not missing/ lose after rebuild project

  • @dracula5752
    @dracula5752 23 дні тому

    how do you disconnect or not allow connect to websockets if they are not authenicated?

  • @ralacerda12
    @ralacerda12 3 місяці тому +1

    This is amazing.
    But if I'm building a chat application I would need to save the messages somewhere (using unstorage), right?
    If I have a array on the top of the 'websocket.ts' file, will that be avaliable for all instances of the websocket?
    Also, I'm assuming this cannot be deployed on serverless, right?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      Yes, saving would be helpful, eg in a KV store or even a database. Check out Nitros db0 layer or unstitched as you mentioned
      A external variable would work as „in-memory storage“ but I’d not recommend that as you can’t share it across instances (Better use a kv then)

  • @MissedWarrior
    @MissedWarrior 3 місяці тому

    Firefox also has WebSocket debugging tool in response tab but since there were no messages after connection it simply showed that there were no messages 😅 Thanks for the vid!

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      Good point! Thanks for the heads up 😊

  • @Gornius
    @Gornius 3 місяці тому +1

    Can you do the same with Server Sent Events? It's uni-directional (server-to-client), but works on the same protocol (HTTP).

  • @user-wz4zm1if6f
    @user-wz4zm1if6f 3 місяці тому

    Really cool video. can you do a video about Nuxt test?

  • @sematoghem
    @sematoghem 3 місяці тому

    finally... this feature is so simple but long time to do. you can use socketio but it so not native and the bad thing is it work in dev but not in prod!!!!

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому

      It is actually pretty "tricky" given the runtime-agnostic approach. In "node only" it'd be simple though, yes.

  • @TheDigitalSight
    @TheDigitalSight 3 місяці тому +1

    Can you also create video on consuming streaming responses for text & video? For example, How to render responses from OpenAI api or local LLMs?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      On the list 👍🏻

    • @TheDigitalSight
      @TheDigitalSight 3 місяці тому +1

      If possible will you create reusable composable we can use? or even better if possible integrate in nuxt core@@TheAlexLichter

    • @TheDigitalSight
      @TheDigitalSight 3 місяці тому

      Also I'll be using OpenAI api next week so may I know when you'll be uploading video for it if possible?@@TheAlexLichter
      Thanks for teaching gems in nuxt ♥

  • @trinhnam3117
    @trinhnam3117 3 місяці тому

    Your video is very helpful, thank you. Can you share how to handle authentication with Nuxt3, I'm looking forward to it ^^

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      Glad to hear 🙏🏻
      Auth is on the list, yes. But unfortunately a *very broad* topic to cover. So many ways to do auth 🙈

  • @Shulkerkiste
    @Shulkerkiste 3 місяці тому

    Firebase is listed at 1:20
    In which way will it be supported?
    Does Firebase support Websockets?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      Firebase is listed as "needs investigation", meaning no guaranteed support at the time of recording
      Follow along github.com/unjs/nitro/issues/2171 (+ get involved if possible! 🙌😊)

  • @jurarexx
    @jurarexx 2 місяці тому

    Is there a way to send message to websocket from server-side?

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому +1

      You probably can by setting up a node websocket client, sure.

  • @leestevenscoder
    @leestevenscoder 3 місяці тому

    Great stuff. Many thanks to Alex for being so cutting edge with Nuxt. But does anyone know why "useWebSocket is not defined"? Even when i get the same output from `pnpm why nuxi-nightly` as the video.

    • @leestevenscoder
      @leestevenscoder 3 місяці тому

      because I need to `pnpm i @vueuse/core` and import from that

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому +1

      Should be auto imported when using the nuxt module (shown in the config beforehand) ☺️

    • @leestevenscoder
      @leestevenscoder 3 місяці тому

      Yes. There it is 😂

  • @Zyk0tiK
    @Zyk0tiK 3 місяці тому

    I downloaded your example code from GitHub but I am unable to get it to connect to websockets with npm run dev, I have to build it and run it with node. Any ideas?

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      As mentioned from 00:50 on, make sure you use the nightly build of nuxt + nuxi which are necessary at the moment 😊

    • @Zyk0tiK
      @Zyk0tiK 3 місяці тому

      @@TheAlexLichter I used your code from github and did an npm install on it. The package.json says it's using nuxt-nightly... am I missing something else?
      Aha, I see. `npx nuxi-nightly dev` to run it. All good!

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      It should also run fine with `npm run dev` in there 😊

  • @iUmerFarooq
    @iUmerFarooq 3 місяці тому

    Why we can't call https api in nuxt server. It gives ssl issue. How and when it will solve?

    • @svenvanreenen
      @svenvanreenen 3 місяці тому

      I can without issues. $fetch working great in nitro server.

    • @TheAlexLichter
      @TheAlexLichter  3 місяці тому

      You might need NODE_TLS_REJECT_UNAUTHORIZED='0', see e.g. github.com/nuxt/nuxt/issues/26222

    • @iUmerFarooq
      @iUmerFarooq 3 місяці тому

      @@TheAlexLichter Yeah it's work.
      If possible please make a tutorial on it.
      Thanks a lot 💚

  • @QueeeeenZ
    @QueeeeenZ 3 місяці тому +1

    Can you do a video about Nitro Tasks?

  • @some_body_qtyeeuy
    @some_body_qtyeeuy 3 місяці тому +1

    Здарова Лёха!

  • @user-sj7tf2yv3m
    @user-sj7tf2yv3m 3 місяці тому

    Не надо дядя, не надо 😮

  • @DougLopes
    @DougLopes 2 місяці тому

    holy 4 min of explanation and config to use this, doesn't look trustable

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому

      How long do you usually spend time reading the docs + implementing the learnt knowledge? I bet more than 4 minutes 😁

    • @DougLopes
      @DougLopes 2 місяці тому

      @@TheAlexLichter absolutelly yes, just saying that looks unsafe for prod if is so experimental.

    • @TheAlexLichter
      @TheAlexLichter  2 місяці тому +1

      Ah well, a fair point to use it carefully but it works fine! Just make sure to pin the version in case changes come 🙌🏻