JavaScript Node.js, Socket.io emit not working node socket emit,node js socket emit,node js socket

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code.

var io = require("socket.io").listen(server);

//Some external codes for running server in Node.js

io.on("connection", function(socket) {
    //This line is working.
    console.log("Socket connected.");

    io.on("requestNewMap", function(data) {
        //Create new map object in here.
        io.emit("responseNewMap", { mapData: map });
    });
});

And this is my client side javascript code.

var socket = io();

socket.on("responseNewMap", function(data) {
    var map = data.mapData;

    //Draw map in canvas's context.
});

//setInterval runs this method every 2 seconds.
function requestNewMap() {
    socket.emit("requestNewMap");
}
Answer:1

This part could be wrong:

io.on("requestNewMap", function(data) {
    //Create new map object in here.
    io.emit("responseNewMap", { mapData: map });
});

I would use socket there as in:

socket.on("requestNewMap", function(data) {
    //Create new map object in here.
    socket.emit("responseNewMap", { mapData: map });
});

I think io.emit should work fine, it would just send the response to every connected client, but io.on('requestNewMap',...) won't work since requestNewMap is not an io standard event.

Answer:2

change this

io.on("requestNewMap", function(data) {
    //Create new map object in here.
    io.emit("responseNewMap", { mapData: map });
});

into this

socket.on("requestNewMap", function(data) {
    //Create new map object in here.
    socket.emit("responseNewMap", { mapData: map });
});

you are adding an event listener to the server object not the client socket.

Answer:3

I'm not certain, but I believe your problem is that your function on the client side is written like you are expective 'requestNewMap' to be passing along data. Instead of:

io.on("requestNewMap", function(data){

Try:

io.on("requestNewMap", function(){

OR pass an empty object or some other kind of junk data along with the emit from client side like this:

function requestNewMap() {
  socket.emit("requestNewMap", {});
}

function requestNewMap() {
  socket.emit("requestNewMap", undefined);
}

function requestNewMap() {
  socket.emit("requestNewMap", -1);
}

Hopefully this is helpful and correct!

Edit: This turned out to not be the answer to your question, but still something worth considering. If you're not passing data along you should avoid writing the event handler as if it expects data.

Answer:4

I'm trying to play a video that I've CENC-encrypted using MP4box with these options: MP4Box -crypt drm_file_gpac_clear.xml BigBuckBunny.mp4 -out BigBuckBunny_cenc.mp4 Where the XML file specifies ...

I'm trying to play a video that I've CENC-encrypted using MP4box with these options: MP4Box -crypt drm_file_gpac_clear.xml BigBuckBunny.mp4 -out BigBuckBunny_cenc.mp4 Where the XML file specifies ...

I am using axios for AJAX in Vue. In the article written by You, he mentioned that we can set Vue.prototype.$http = axios and I can use this.$http in Vue instance. It works fine. However, if I want ...

I am using axios for AJAX in Vue. In the article written by You, he mentioned that we can set Vue.prototype.$http = axios and I can use this.$http in Vue instance. It works fine. However, if I want ...

  1. create instance axios
  2. create axios instance react
  3. create axios instance vue

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...

from the lodash documentation: Throttle Creates a throttled function that only invokes func at most once per every wait milliseconds Debounce Creates a debounced function that delays ...

from the lodash documentation: Throttle Creates a throttled function that only invokes func at most once per every wait milliseconds Debounce Creates a debounced function that delays ...

  1. difference between throttle and debounce
  2. difference between throttle body cleaner and carb cleaner
  3. difference between throttle and choke
  4. difference between throttle body and fuel injection
  5. difference between throttle body and carburetor
  6. difference between throttle body and carburetor cleaner
  7. difference between throttle and accelerator
  8. difference between throttle and nozzle
  9. difference between throttle governing and nozzle governing
  10. difference between throttle body and egr valve
  11. difference between throttle and idle cable
  12. difference between throttling and expansion valve
  13. difference between throttle body cleaner and brake cleaner
  14. difference between throttle and mixture
  15. difference between throttle and pedal assist
  16. difference between throttle valve and butterfly valve
  17. difference between throttle and thrust
  18. difference between throttle and strangle
  19. difference between throttle and propeller
  20. difference between throttle body and fuel injector