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

@nuxt-alt/websocket

v0.0.4

Published

Socket.io WebSocket module for Nuxt

Downloads

11

Readme

Socket.io WebSocket module for Nuxt

Info

This is a socket.io module for Nuxt 3. The project was originally supposed to imitate a Pull Request, but I found that the implementation while it was working, needed a lot of setup and ws is barebones so a lot of custom functionality would be needed to be added. So I opted for socket.io instead. Plus hadling events via the event handler seemed tedious.

Setup

  1. Add @nuxt-alt/websocket and @nuxt-alt/proxy dependency to your project
yarn add @nuxt-alt/websocket @nuxt-alt/proxy
  1. Add @nuxt-alt/websocket to the modules section of nuxt.config.ts
export default defineNuxtConfig({
    modules: [
        '@nuxt-alt/websocket',
        '@nuxt-alt/proxy'
    ],
    proxy: {
        experimental: {
            listener: true
        }
    }
    websocket: {
        // websockets
        websockets: {}
    }
});
  1. Note: You do not need to define @nuxt-alt/proxy in your module array (but it does need to be added as a package), if it's not there it will be automatically added for you with the experimental.listener property set.

Development

Running tests for development:

$ yarn install
$ yarn dev

or (if you want a feel of how it would work in production - since that what this module aims for)

$ yarn install
$ yarn dev:build
$ yarn dev:preview

Options

websockets

interface WebSocketOpts {
    /**
     * The name of the socket.io handler this is used to access the server 
     * through the socket.io instance globalThis.$io[name] in nitro. This 
     * field is required.
     * 
     * 
     * *Note:* globalThis.$io[name] is not avaialable in dev mode due to the fact that the 
     * nitro listen even outputs a sock file instead of the http server so socket.io has
     * nothing to latch on to. You may use the `events` property to register event functions
     * for socket.io in dev mode.
     */
    name: string

    /**
     * Enabled by default, this handles the nitro routing for the server response.
     * Disable this if you want to do this yourself or for any other reason that 
     * you might need it disabled.
     */
    handler?: boolean

    /**
     * You can enter a series of functions here to handle the events for the socket.io instance
     * or you may use the globalThis.$io[name] instance in nitro if you prefer.
     */
    events?: {
        [key: string]: ((io: Server, runtimeConfig: NitroRuntimeConfig) => void) | undefined
    }

    /**
     * Server Options for the socket.io instance. The `path` property is omitted because it's always
     * overridden by the `websockets` object keys.
     */
    serverOptions?: Omit<Partial<ServerOptions>, 'path'>
}

Config Example

import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
    modules: [
        '@nuxt-alt/websocket',
    ],
    proxy: {
        websockets: {
            '/socket.io': {
                // Required property
                name: 'main',
                events: {
                    // nitro runtime config only works in production
                    // otherwise it uses nuxt runtime config in dev
                    test: (io, config) => {
                        io.on('connection', () => {})
                    } 
                }
            },
        }
    }
})

Nitro

You are able to use hooks to access an instance of socket.io based on the name you specified in the websockets config. The types for the names will automatically be generated. Each socket.io instacnes can be accessed with the prefix io:. If you'd rather opt to use the $io global instance, do note that it's only avaialbe inside the listen:node nitro hook as that's where it's first registered. So you either have to wait for the registration or use it inside the hook.

  1. With hook:
export default defineNitroPlugin(({ hooks, h3App }) => {
    hooks.hook('io:main', (io) => {
        io.on('connection', socket => {
            console.log(socket.id)
        })
    })
})
  1. With global instance:
export default defineNitroPlugin(({ hooks, h3App }) => {
    hooks.hook('listen:node', (server) => {
        // can be in the form of $io[''] or $io.
        $io.main.on('connection', socket => {
            console.log(socket.id)
        })
    })
})

Composables

A useIO composable is avaialble to use otherwise you can use $io from useNuxtApp() or import the io module from socket.io-client directly.

Known Issues

  • Polling will not work with the module (sometimes, its really finicky in nitro). By default all socket.io instances in nitro uses websockets without polling. if you'd like to take a crack at handling this yourself you can disable the handler of your socket.io instance. This will disable the event handler for the websockt path and if there's no handler it will lead to 404s (doesn't happen all the time but it happens enough times to have it disabled). You can also disable it if you end up wanting to use the proxy module to handle proxying the request.

  • Socket event functions dont use nitro runtime config in dev mode and the nitro runtime hooks for accessing the $io instance in nitro. This is a limitation of nitro being inaccessible to the nuxt instance and vice versa. You can still utilize the socket event functions.