Socket with strapi #5872

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>