I want to connect a NodeMCU Lua socket client to node.js socket.io server.
NodeMCU Lua code:
sk = net.createConnection(net.TCP, 0)
sk:on("receive", function ( sck,c )
print (c)
end)
sk:on("connection", function ( sck,c )
print("Connected")
sk:send("Helloooo...")
end)
sk:connect(12346,"192.168.1.100")
Node.js server code:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('someone is connected');
});
server.listen(12346);
The problem:
The on connection event in the Lua client is fired and prints "Connected", but the on connection event in node.js socket.io server isn't fired. I tried the Lua client with a Python socket server and it worked well! And I also tried a node.js socket server with a Javascript socket client and it worked well!
Are there compatibility problems between NodeMCU and socket.io?
Socket.io is a WebSocket wrapper, not a basic socket implementation. There are some specific operations in it such as handshaking and heartbeat. So you can succeed with socket servers but not with a WebSocket one.
You may use a WebSocket client implementation as well on the NodeMCU side. But I am not sure if the Lua library matches with the WebSocket API version.
If you want async communication, you may use MQTT, which has also lots of libraries for NodeJS. Otherwise use the socket server of NodeJS as you have done successfully previously.