Not able to connect to a TCP client inside meteor app from an outside TCP server


#1

Hi, I am trying to make a tcp connection from an external server to an internal tcp client. but not able to do so.

Please see the below code:

client.js

var net = require('net');
var client = new net.Socket();
client.connect(4000, '127.0.0.1', function() {
console.log('connected');

});

client.on('data', function(data) {
//console.log('Received': + data);
//console.log(JSON.parse(data).name);
//console.log(JSON.parse(data).serverName);
//console.log(JSON.parse(data).msg);

client.write('bro done');

});

server.js (externally located)

var net = require('net');
var server = net.createServer(function(socket) {
socket.write('{"name": "manish", "age": "934", "channel": "general", "serverName": "myServer", "msg": "Say hello"}');
socket.on('data', function(data) {
console.log(JSON.stringify(data));
})
});

server.listen(4000, '127.0.0.1');


#2

Your code works fine for me. You did put your “client” code on the server? It’s not a client in the Meteor sense of a browser app - it’s a client in the sense of a server application talking to another server application listening over a socket.

I20180315-10:07:53.091(0)? connected
I20180315-10:07:53.147(0)? Received <Buffer 7b 22 6e 61 6d 65 22 3a 20 22 6d 61 6e 69 73 68 22 2c 20 22 61 67 65 22 3a 20 22 39 33 34 22 2c 20 22 63 68 61 6e 6e 65 6c 22 3a 20 22 67 65 6e 65 72 ... >
I20180315-10:07:53.148(0)? manish
I20180315-10:07:53.148(0)? myServer
I20180315-10:07:53.149(0)? Say hello
I20180315-10:07:53.098(0)? {"type":"Buffer","data":[100,111,110,101]}


#3

I am actually trying to put the client and the server as a package within Meteor (in a browser app)


#4

Someone will correct me if I’m wrong, but I don’t think you can use that code directly in a browser client.

You could use Meteor Methods and the pub/sub API to pass results back and forth from the browser to the server, with the server doing the socket work. I guess the success of that depends on how intensive for DDP that is likely to be.

Alternatively, look at the net-browserify package.