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

@addoinc/strapi-plugin-io

v1.1.2

Published

A plugin for Strapi CMS that provides the ability for Socket IO integration

Downloads

3

Readme

strapi-plugin-io

A plugin for Strapi CMS that provides the ability for Socket IO integration

Downloads Install size Package version

Requirements

The installation requirements are the same as Strapi itself and can be found in the documentation on the Quick Start page in the Prerequisites info card.

Supported Strapi versions

  • v4.x.x

NOTE: While this plugin may work with the older Strapi versions, they are not supported, it is always recommended to use the latest version of Strapi.

Installation

npm install strapi-plugin-io

# or

yarn add strapi-plugin-io

Configuration

The plugin configuration is stored in a config file located at ./config/plugins.js.

Sample configuration

module.exports = ({ env }) => ({
  // ...
  "io": {
    "enabled": true,
    "config": {
      "IOServerOptions" :{
        "cors": { "origin": "http://localhost:5000", "methods": ["GET"] },
      },
      "contentTypes": {
        "message": "*",
        "chat":["create"]
      },
      "events":[
        {
          "name": "connection",
          "handler": ({ strapi }, socket) => {
            strapi.log.info(`[io] new connection with id ${socket.id}`);
          },
        },
      ]
    },
  },
  // ...
});

The above configuration will do the following:

  1. Set the IO server options.
  2. Emit all events for the messages content type and only the create event for the chats content type.
  3. Log the socket id of any newly connected sockets.

IMPORTANT NOTE: Make sure any sensitive data is stored in env files.

The Complete Plugin Configuration Object

| Property | Description | Type | Default | Required | | -------- | ----------- | ---- | ------- | -------- | | IOServerOptions | The Socket IO Server Options. | Object | {} | No | contentTypes | The Content Types to emit events for. A "" can be used to listen for all content types and events. | Object or String | {} | No | | contentTypes[apiName] | The events to listen for on the given content type. The apiName is the singularName in the model schema. The value can be an array of events or a "" to listen for all. | Array or String | N/A | Yes | | events | Any server server side events to listen for | Array | [] | No | | events[x].name | The event name (case sensitive) | String | N/A | Yes | | events[x].handler | The function to execute when the event is triggered. The handlers first argument is the ctx containing the strapi instance, the subsequent parameters are in the same order as in the client emit event. For the built in "connection" event the subsequent parameter is the socket. | Function | N/A | Yes | publicRoleName | The name of the default role to be assigned to unauthenticated connections | String | "Public" | No

Currently Supported Emit events

  • find
  • findOne
  • delete
  • create
  • update
  • publish
  • unpublish
  • createOrUpdate (single content type only)

Emit Syntax

All events emitted have the following syntax singularName:action.

Emitting Custom Events

The server socket instance is attached to the strapi object. Custom event can be emitted via the emit or raw method.

Emit

The emit function accepts a specific event format (api::singularName.singularName.actionName) and will automatically verify permissions for all strapi roles (rooms).

strapi.$io.emit("api::message.message.someCustomAction", data);

Raw

A raw emit will not verify any permissions before emitting.

Raw emit to all rooms

strapi.$io.raw("customEvent", data);

Raw emit to a specific room

strapi.$io.raw("customEvent", data, {room: "SOCKET_ID"});

Example Client Connections

Below are example client socket configurations.

Unauthenticated User connection

const { io } = require("socket.io-client");
const SERVER_URL = "http://localhost:1337";
const socket = io(SERVER_URL);

//  wait until socket connects before adding event listeners
socket.on("connect", () => {
  socket.on("message:update", (data) => {
    console.log(data)
  });
});

Authenticated User connection

const { io } = require("socket.io-client");
const SERVER_URL = "http://localhost:1337";
const JWT_TOKEN = "your users JWT token";

// token will be verified, connection will be rejected if not a valid JWT
const socket = io(SERVER_URL, {
  auth: { 
    token: JWT_TOKEN
  },
});

//  wait until socket connects before adding event listeners
socket.on("connect", () => {
  socket.on("message:update", (data) => {
    console.log(data)
  });
});

Bugs

If any bugs are found please report them as a Github Issue