A plugin for Strapi CMS that provides the ability for Socket IO integration
A plugin for Strapi CMS that provides the ability for Socket IO integration
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.
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.
npm install strapi-plugin-io
# or
yarn add strapi-plugin-io
The plugin configuration is stored in a config file located at ./config/plugins.js
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:
IMPORTANT NOTE: Make sure any sensitive data is stored in env files.
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 |
contentTypesapiName | 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 |
eventsx.name | The event name (case sensitive) | String | N/A | Yes |
eventsx.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 |
find
findOne
delete
create
update
publish
unpublish
createOrUpdate
(single content type only)All events emitted have the following syntax singularName:action
.
The server socket instance is attached to the strapi object. Custom event can be emitted via the emit or raw method.
The emit function accepts a specific event format (api::singularName.singularName.actionName) and will automatically verify permissions for all strapi roles (rooms).
1
strapi.$io.emit("api::message.message.someCustomAction", data);
A raw emit will not verify any permissions before emitting.
1
strapi.$io.raw("customEvent", data);
1
strapi.$io.raw("customEvent", data, {room: "SOCKET_ID"});
Below are example client socket configurations.
1
2
3
4
5
6
7
8
9
10
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)
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
});
});
If any bugs are found please report them as a Github Issue
npm install strapi-plugin-io
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.