Socket with strapi #5872

This discussion has been migrated from our Github Discussion #5872

maktoobgar247d ago

First of all thanks for creating strapi…

Describe the bug

There is a tutorial about how to use strapi with side by side but this structure only workes in strapi alpha vesion 3.0.0-alpha.10.1.
So first time i tried to use it on the latest strapi release 3.0.0-beta.20, I did exactly everything that happened in the tutorial (the link in Resourse topic) but strapi answers to all client socket requests with 404 response.

Steps to reproduce the behavior

  1. As tutorial, I created a new strapi project.
  2. Made a model with “bakery” name and two fields in it (name: string and rating: integer).
  3. Then i had to give create and find public permission for bakery model to access the model from client side.
  4. Then add this code to config/functions/bootstrap.js:

module.exports = () => { var io = require(‘’)(strapi.server); io.on(‘connection’, function(socket){ socket.emit(‘hello’, JSON.stringify({message: ‘Hello food lover’})); socket.on(‘disconnect’, () => console.log(‘a user disconnected’)); }); = io; };

  1. Then create public/bakery.html and add this code down here to it:

<!doctype html> bakery

Expected behavior

So after these steps your are going to see your bakery.html windows in your model address localhost:/bakery in strapi alpha version but…

Unexpected behavior

…in beta there is just 404 not found responses that strapi gives it to me.
strapi exactly blocks functionality and answers all the requests with 404.


result in strapi beta version:
Image of Yaktocat


  • Node.js version: 12.16.2
  • NPM version: 6.14.4
  • Strapi version: 3.0.0-beta.20
  • Operating system: Ubuntu 18.04.4 LTS


And this is the tutorial i’m talking about:

Responses to the discussion on Github

derrickmehaffy247d ago


Sockets are not part of Strapi, thus this isn’t a bug report. I am moving this over to a discussion

maktoobgar247d ago


I just wanted an answer from you guys(the developers). I know this is not bug but i send it as a bug report to take a response from developers. If your focus is not on sockets at all… really no problem. You guys are awesome.

derrickmehaffy247d ago


<— Not a developer just part of the community that helps clean issues. Questions are in the process of being moved here to discussions. So in the future please post non-bug reports here in discussions.


alexkharech244d ago

Did you manage to connect how did you do that?

ronildo238d ago

I didn’t manage to fix this yet, but I know the problem.
In the code below strapi.server is undefined, when this export function is executed the server has not been started yet, so if you use a timeout of 2 seconds, it will be available when this works.

module.exports = () => {
  var io = require('')(strapi.server);
  io.on('connection', function(socket){
    socket.emit('hello', JSON.stringify({message: 'Hello food lover'}));
    socket.on('disconnect', () => console.log('a user disconnected'));
  }); = io;

marefati110243d ago

change strapi version to 19.5
and replace index.html with this (its work for me)

<!doctype html>
    <meta charset="utf-8" />
    <!-- add script -->
    <script src=""></script>
      // connect user throught socket
      var socket = io.connect('http://localhost:1337');
      // listen for event name 'hello' & log it
      socket.on('hello', (res) => console.log(res));