npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xarenas107/nuxt-socket-io

v3.1.0

Published

Nuxt module for socket.io

Downloads

9

Readme

Nuxt Socket.io

npm version npm downloads License Nuxt

A Nuxt module for socket.io

🚀 Features

  • Nuxt 3 ready
  • 👌 Zero config: Configuration for common use cases
  • Nitro ready: Integration with nitro server.
  • 🍍 Pinia ready: Integration with @pinia/nuxt module.
  • ⚙️ Auto import: Provide auto import functions for client and server side.
  • 🪝 Customizable: Provide hooks for customization on client and server side.
  • 🦾 Type strong: Written in typescript

📦 Install

npm i @xarenas107/nuxt-socket-io -D 

🦄 Usage

Add @xarenas107/nuxt-socket-io to the modules section of nuxt.config.ts.

export default defineNuxtConfig({
  modules: [
    '@xarenas107/nuxt-socket-io'
  ]
})

Extends socket.io configuration with nuxt hooks.

| Hook | Argument | #Enviroment | Description | |------------------|------------------|-------------|----------------------------------------------| | io:server:config | server options | server | Called before configuring socket.io server | | io:server:done | socket server | server | Called after socket.io server initialization | | io:config | client options | client | Called before configuring socket.io-client | | io:done | socket | client | Called after socket.io-client initialization |

Use useSocketIO() or useSocketIOStore() on client side and the useSocketIO() on server side.

// On client side
<script lang='ts' setup>
  const io = useSocketIOStore() // Prevent duplicated listener
  io.on('pong',(message:string) => console.log(message))
  
  await $fetch('/api/ping')
</script>
// On server side
export default defineEventHandler(event => {
  // You can access to io server through `event.context.io` or `useSocketIO().server`
  const io = useSocketIO(event)
  
  const id = io.getId()
  if (id) io.to(id,'pong','Response from server')
  return
})

Or you can create your own methods

// On server side plugin `server/plugins/wss.ts`
export default defineNitroPlugin(nitro => {

  // Add nitro hook
  nitro.hooks.hook('render:response', (response,{ event }) => {
    const { session, user, io } = event.context
    const uid = user?.id || session?.user?.toString()

    // Add connection listener
    if (!event.context.io?.server?.sockets?.adapter?.rooms?.has(uid)) {
      io.server.on('connection', socket => {
        if (!uid) return
        socket.join(uid)
        socket.on('disconnect',() => socket.leave(uid))
      })
    }
  })
})
// On server side middleware `server/middleware/wss.ts`
import type { Server } from 'socket.io'

export default defineEventHandler(event => {
  const { session, user, io } = event.context
  const uid = user?.id || session?.user?.toString()

  event.context.io.emit = (event, message) => {
    return io.server.to(uid).compress(true).emit(event, message)
  }
})

declare module 'h3' {
  interface SocketH3EventContext {
    emit: Server['emit']
  }
}
// On server handler `server/api/chat.post.ts`
export default defineEventHandler(event => {
  const body = await readBody(event)
  
  // Send data to client
  const io = useSocketIO(event)
  io.emit('message', body)

  return null
})

That's it! You can now use @xarenas107/nuxt-socket-io in your Nuxt app ✨