1

I am working on a node JS application, where I am trying to use socket.io following this tutorial. Until this tutorial everything is fine, even the client is connected to the server through the socket, as it display a message on connection. But I don't know why my code isn't working on emit, and on event, and event handler.

Below is my Code on server side :

const express = require("express"); const app = express(); const scrap = require("./algorithm"); const mysql = require("mysql"); const ms_connect = mysql.createConnection({ host:'localhost', user:'root', password:'', database:'scrapper_db' }); const server = app.listen(8000, function(){ console.log('Listening on 8000'); }); const io = require("socket.io").listen(server); app.use(express.static(__dirname + "/")); io.on("connection",function(socket){ console.log("Sockets Connection Made ! " + socket.id); socket.emit("testing",{data:"I am tested"}); io.on("disconnect",function(){ console.log("Client Disconnected !"); }) }) //mySQL Conection ms_connect.connect(function(err){ if(err) console.log(err); ms_connect.query("Select * FROM test",function(err,rows,fields){ if(err) console.log("Error Executing Query"); }) }) app.get("/scrap",function(req,res){ res.sendFile(__dirname+"/index.html"); })

Client side code :

var socket = io.connect('http://localhost:8000/scrap'); console.log(socket.connected); //returns false :( socket.on("testing", function(d) { console.log(d); });

In the client side, the socket.connected object returns false, but on server side it says connected. I don't know how , and I am using third link from this socket.io cdnjs server.

7
  • Why are you putting scrap on the client side? localhost:8000/scrap Commented Aug 8, 2018 at 19:59
  • This is the route for loading my index.html file from app.get("/scrap",func) @RaphaelFacredyn Commented Aug 8, 2018 at 20:02
  • 1
    You should try using socket.on('connect', () => { console.log(socket.connected); // true }); rather than just logging socket.connected Also what happens if you just use var socket = io.connect(); on your client side code Commented Aug 8, 2018 at 20:02
  • Should i use this on client side @TommyBs ? Commented Aug 8, 2018 at 20:05
  • @Nadeem Ahmad yes Commented Aug 8, 2018 at 20:06

2 Answers 2

1

You are doing io.connect('http://localhost:8000/scrap') but the scrap is not mentioned anywhere on the server side. It should be io.connect('http://localhost:8000/'). Pointing to your HTML file is not needed because the socket.io server and your webserver are unrelated.

Also as pointed out by @TommyBs you should use

socket.on('connect', () => { console.log(socket.connected); }); 

to check if you are connected because connecting is asynchronous so it will not have connected yet by the time you do console.log(socket.connected);

The whole client code would be

var socket = io.connect('http://localhost:8000'); socket.on('connect', () => { console.log(socket.connected); }); socket.on("testing", function(d) { console.log(d); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Change http://localhost:8000/scrap to http://localhost:8000/ in the client code. You're connecting to the wrong route.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.