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 socket.io 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(‘socket.io’)(strapi.server); io.on(‘connection’, function(socket){ socket.emit(‘hello’, JSON.stringify({message: ‘Hello food lover’})); socket.on(‘disconnect’, () => console.log(‘a user disconnected’)); }); strapi.io = 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 socket.io functionality and answers all the requests with 404.

Screenshots

result in strapi beta version:
Image of Yaktocat

System

  • 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

Resource

And this is the tutorial i’m talking about:

Responses to the discussion on Github


derrickmehaffy247d ago

Collaborator

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


maktoobgar247d ago

Author

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

Collaborator

<— 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.

Thanks


alexkharech244d ago

Did you manage to connect soket.io? 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('socket.io')(strapi.server);
  io.on('connection', function(socket){
    socket.emit('hello', JSON.stringify({message: 'Hello food lover'}));
    socket.on('disconnect', () => console.log('a user disconnected'));
  });
  strapi.io = io;
};

marefati110243d ago

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

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>bakery</title>
  </head>
  <body>
    <!-- add socket.io script -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
    <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));
    </script>
  </body>
</html>