1.8.14 • Published 1 month ago

@xarenas107/nuxt-socket.io v1.8.14

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Nuxt Socket.io

npm version npm downloads License Nuxt

My new Nuxt module for doing amazing things.

🚀 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.

HookArgument#EnviromentDescription
socket.io:server:doneserverserverCalled before configuring socket.io server
socket.io:donesocketclientCalled 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.on('disconnect',() => socket.leave(uid))
        socket.join(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 socket = useSocketIO(event)
  socket.emit('message', body)

  return null
})

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

1.8.14

1 month ago

1.8.11

2 months ago

1.8.13

2 months ago

1.8.9

3 months ago

1.8.8

3 months ago

1.8.7

3 months ago

1.8.6

3 months ago

1.8.5

3 months ago

1.8.3

3 months ago

1.8.2

3 months ago

1.8.1

3 months ago

1.8.0

3 months ago

1.7.8

3 months ago

1.7.7

3 months ago

1.7.3

3 months ago

1.7.2

3 months ago

1.7.0

3 months ago

1.6.0

3 months ago

1.5.0

3 months ago

1.7.6

3 months ago

1.7.5

3 months ago

1.7.4

3 months ago

1.4.7

3 months ago

1.4.6

3 months ago

1.4.5

3 months ago

1.4.4

3 months ago

1.4.3

3 months ago

1.4.2

3 months ago

1.4.1

3 months ago

1.4.0

3 months ago

1.3.13

3 months ago

1.3.12

3 months ago

1.3.11

3 months ago

1.3.9

3 months ago

1.3.7

3 months ago

1.3.6

3 months ago

1.3.5

3 months ago

1.3.4

3 months ago

1.3.3

3 months ago

1.3.2

3 months ago