I am trying to implement socket.io into my strapi project and It doesn't work and I don't know why.
I implemented bootstrap.js into config/functions folder.
module.exports = async ({ strapi }) => {
console.log('Bootstrap loaded')
require('../../src/socket-server')({ strapi })
}
I impleneted socket-server.js in src/socket-server.js
const { Server } = require('socket.io')
module.exports = ({ strapi }) => {
const io = new Server(strapi.server, {
cors: {
origin: '*',
},
})
strapi.io = io
io.on('connection', socket => {
console.log('A user connected:', socket.id)
socket.on('sendMessage', data => {
console.log('Message received:', data)
io.emit('message', data)
})
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id)
})
})
}
and I created a hook for my messages
module.exports = {
async afterCreate(event) {
const { result } = event
const io = strapi.io
if (io) {
io.emit('message', result)
}
},
}
but when I try to run a code, bootstrap.js doesn't even console log a string. I was trying everything, but I was a little bit lost.
config/functions
is not a folder where you implement bootstrap
. Bootstrap is implemented in src/index.js