Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!