ReferenceError: client is not defined Socket.io

How to solve this? I don’t get it,,please help, thanks. And this is the error message when I run node server.js:

client.on(‘getRelay’, function (state) {
ReferenceError: client is not defined

var express = require ('express');
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = 3000;

let bodyRelay = {
    relayId: 'RELAY-123',
    state: 1
}

app.use(express.static(__dirname + '/node_modules'));
app.get('/', function(req, res, next) {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(client) {
    console.log('a user has connected');

});

io.sockets.emit('setRelay', bodyRelay);
client.on('getRelay', function (state) {
    bodyRelay = state;
    console.log('id: ' + client.id + 'bodyRelay: ' + JSON.stringify(bodyRelay));
    // io.sockets.emit('setRelay', bodyRelay);
});

client.on('disconnect', function() {
    console.log('a user has disconnected');
});

server.listen(port, () => {
    console.log('Server is running on port: ' + port)
});

Source: Ask Javascript Questions

LEAVE A COMMENT