Use Net for TCP communication


I am trying to open up a socket using net in a cesium sandcastle project derived from Cesium Widget. Cesium Widget displays find, all code below is subsequent to the widget.

Heres the example code I'm trying to get to run:

    var server = net.createServer(function(socket) {
        socket.write('Echo server\r\n');

    server.listen(1337, '');

    var client = new net.Socket();
    client.connect(1337, '', function() {
        client.write('Hello, server! Love, Client.');

    client.on('data', function(data) {
        console.log('Received: ' + data);
        client.destroy(); // kill client after server's response

    client.on('close', function() {
        console.log('Connection closed');

But my problem is in getting 'net' to work. I ran npm install net in the directory of my Cesium installation and restarted the server, but still can't get anything to work with net.

    var net = require('net');
    Uncaught Error: Module name "net" has not been loaded yet for context: _. Use require()

    require(['net'], function (net) {...
yields some kind of script error, and the debugger says it's trying to get net.js out of the Source directory. What?

Anyone have any ideas?


I’m confused. Is this code running under Node on the server? net is a code node module so you should need to npm install anything (and the net module is 3 lines of code that adds net to the global namespace, which you probably don’t want).

If you’re running this code on the client, it won’t work. You need to use WebSockets do do any kind of communications like this on the client.