Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda