Giter Club home page Giter Club logo

nuxt-internal-socket's Introduction

Nuxt Internal Socket

Nuxt module that hooks into the listen hook and creates a socket.io connection.

Why use this module?

You an use this modulke if you want socket functionality in your application but dont want to create a separate socket.io server.

How to use this module?

Follow these steps to get started

  • Install the module
npm install nuxt-internal-socket
  • Add it to yor nuxt.config file
export default defineNuxtConfig({
  modules: ["nuxt-internal-socket"],
});
  • Create your socket function file
mkdir sockets && touch sockets/index.ts
  • Create your socket function
import { Server, Socket } from "socket.io";

export default function (io: Server) {
  io.on("connection", (socket: Socket) => {
    console.log("socket connected", socket.id);
    socket.on("join room", (room) => {
      socket.join(room);
    });
    socket.on("disconnect", (reason) => {
      console.log("socket disconnected");
      io.emit("user disconnected", socket.id);
    });
  });
}
  • Add your socket function to the nuxt.config file & pass params to the module
import { defineNuxtConfig } from "nuxt";
import functions from "./sockets/index";

export default defineNuxtConfig({
  modules: ["nuxt-internal-socket"],
  socketIO: {
    socketFunctions: functions,
  },
});

What this module does?

This module will hook into the listen hook and create a socket.io connection.

You can access the socket client instance through the useNuxtApp composable withing your application.

// get socket from nuxt instance
const { $io } = useNuxtApp();

The in a function you can use the socket client to emit events to the server.

$io.emit("event", { data: "hello" });

nuxt-internal-socket's People

Contributors

baybreezy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.