0.3.1 • Published 1 month ago

nuxt3-socket.io v0.3.1

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

nuxt3-socket.io

!WARNING
This module will soon be deprecated in favor of the built-in WebSocket API.

Just another socket.io module for Nuxt 3.

This module uses the Vite server in development and req.socket.server in production.

Install

pnpm add nuxt3-socket.io
export default defineNuxtConfig({
  modules: ['nuxt3-socket.io'],
  socket: {
    // JSON serializable options only.
    // options object to pass when instantiating socket server.
    serverOptions: {}
  }
})

Usage

Client

<script setup>
// Default
const socket = useSocket()

const connected = ref(false)

onMounted(() => {
  socket.on('connect', () => {
    connected.value = socket.connected
  })

  socket.on('disconnect', () => {
    connected.value = socket.connected
  })
})

// Custom
const { $io } = useNuxtApp()

onMounted(() => {
  const socket2 = $io('http://localhost:3069')
})
</script>

<template>
  <div>Connected: {{ connected }}</div>
</template>

Server

By default, this module automatically creates a server instance. If you want access to that server instance, you can expose functions inside server/socket and use the defineIOHandler wrapper function:

// server/socket/example.ts
import { defineIOHandler } from 'nuxt3-socket.io/helpers'

export default defineIOHandler((io) => {
  io.on('connection', (socket) => {
    console.log('Connected ', socket.id)
  })
})

It's recommended to roll your own socket server if you need more customization other than what is specified here.

Development

  • Run npm run dev:prepare to generate type stubs.
  • Use npm run dev to start playground in development mode.

License

MIT

0.3.1

1 month ago

0.3.0

5 months ago

0.2.1

5 months ago

0.2.0

11 months ago

0.1.0

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.1

1 year ago