Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
Socket.io on connect not working

I've seen this problem before and I've read through all the docs I can find, but somehow I'm just not seeing it in my code. It must be something very trivial I am getting wrong, but any help would be appreciated.

The issue is that I can't get socket.io to connect to the server. On the server side the 'connection established' shows up, but none of my other console logs on server or client sides. I've followed the documentation of the socket.io folks pretty closely.

Here is the server side (in node.js/express btw):

module.exports = function(server){
  console.log('what is server', server);
  var io = require('socket.io')(server);
  var ss = require('socket.io-stream');
  var fs = require('fs');
  var serveIndex = require('serve-index');
  var path = require('path');


io.on('connection', function(socket) {
  console.log('connection established');
  io.sockets.on('songName', function(data) {
    console.log('in back side socket.on');
    var stream = ss.createStream();
    var filename = serveIndex('/playlist/', path.basename(data.name), '.mp3');
    res.writeHead(200, {
      'Content-Type': 'audio/mpeg' || 'audio/mp4',
      'Content-Length': stat.size
    });
    ss(socket).emit('audioStream', stream, { name: filename });
    fs.createReadStream(filename).pipe(stream);
  });
});
};

And here is the client side (and sanity check2 console log fires):

console.log('sanity check2');

$(document).ready(function(){

  window.AudioContext = window.AudioContext||window.webkitAudioContext;
    context = new AudioContext();

  var socket = io.connect('http://localhost:5000/socket');

  socket.on('connection', function(){
    console.log('front end socket connected succesfully');
    socket.emit('songName', {name: 'LaNegra'});
  })

  socket.on('audioStream', function(stream) {
    console.log('in front side socket.on');
    var source = context.createMediaStreamSource(stream);
    source.connect(context.destination);
  });


});

Again, this is a pretty simple problem most likely, so I apologize in advance.

EDIT: Sorry I should have mentioned this, but the server side code is in a file that is /sockets/socket.js and in app.js I call it using app.use().

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda