I have this code working for receiving data from my Arduino but I will like to send data back to my Arduino and get a response on my client page. I added a listening function but I keep getting io.on is not a function
when I send data from my client page.
test.js
io.listen(app.listen(3000)).on('connection', function (client) {
// store client into array
clients.push(client);
// on disconnect
client.on('disconnect', function() {
// remove client from array
clients.splice(clients.indexOf(client), 1);
});
// I added this to listen for event from my chart.JS
io.on('connection', function(socket){
socket.on('LED on', function (data) {
console.log(data);
});
socket.on('LED off', function (data) {
console.log(data);
});
});
});
Your value of
io
is not what it should be.The usual way of doing things is like this:
But I'm guessing that your value of
io
is something like this:That's not the same thing. That's the module handle. But, when you do it this way:
Then,
io
is a socket.io instance. You can bind listeners to an instance, not to the module handle.In every single socket.io server-side example on this doc page, they use one of these forms:
with this:
Nowhere do they do:
That's just the wrong value for
io
.Long story, shortened, you need to fix what you assign to
io
to be consistent with the docs. It's the return value fromrequire('socket.io')(app);
that gives you a socket.io instance object that you can then set up event handlers on.