• Hi everybody,

    Do we have an option to cleanup socket object without a hard reset?

    I'm connecting to a server with a tricky path and I need to try to reconnect with net.connect on any error. But this process consumes memory on every attempt.

    Minimal pseudo-like code is as follows:

    function MyClass(....){
        this.socket = null 
    MyClass.prototype.connect = function (){
        this.socket = net.connect(....)
    var my = new MyClass(..)
  • You should just be able to close the socket.

    If it's not freeing then it's probably because it's referenced from somewhere else in your code?

  • You mean by this.socket.close()? It's not working since Uncaught Error: Function "close" not found!.

    Here is my current workaround: Before calling net.socket(), I delete all members of MyClass.socket manually:

    for (var member in this.socket) delete this.socket[member];
    this.socket = net.connect(...


    This workaround seemed to free some memory but obviously does not prevent old sockets from connecting server when possible, hence consuming memory. (I don't understand what is going on...)

  • @ceremcem, that you get the not found indicates that the context where you invoke it may not be MyClass... what is the value of this.socket? Your work around may only free referenced things, which is not good enough. The .close() most likely calls some functions that remove 'things' from some other objects...

  • @allObjects


    ={ "type": 0, 
      "#onconnect": function (e) { ... }, 
      "opt": { 
        "host": "example.com", 
        "port": 1234 }, 
      "sckt": 18, "conn": true, 
      "#ondata": function (e) { ... }, 
      "#onerror": function () { ... }, 
      "#onclose": function () { ... }, 
      "#onend": function () { ... }, 
      "dSnd": "", 
      "dRcv": undefined }

    my.socket.write("hehe") actually sends "hehe" to the server.


    Uncaught Error: Function "close" not found!
     at line 1 col 11

    But, Socket.end() seems to work as expected, with an acceptable delay.

  • Hi,
    I am using an Espruino Wifi.
    Copied the example from https://www.espruino.com/WebServer and had to change:
    Wifi --> Espruino_Wifi and defined a password in wifi.startAP('Espruino_server', {password:'xxxxxxxx'}, startServer);
    After that I saved to code with save()

    WebServer error InternalError {
    "msg": "Unable to create socket\n",
    "type": "InternalError",
    "stack": " at line 2 col 29\nthis.server.listen(this.port),this.e­mit(\"start\",this)}catch(...\n ^\n"
    It has been working a few days ago.
    Could you tell me what is happening?

  • Have to mention:
    Also after a reset and reflash nothing changes....

  • If you stick the Web server creation into an onInit() { ... } function, you should be fine.

    var WS = require ("WebServer"), ws;
    function onInit() {
      ws  = new WS (...
  • If you stick the Web server creation into an onInit() { ... } function, you should be fine.

    var WS = require ("WebServer"), ws;
    function onInit() {
      ws  = new WS (...

    This way, of course, your Web server will not star on upload of the code... You start it by entering onInit() in the console. When done with development, save() the code, and any reset and repower will do fine too.

    Take a look at 2 cents details in conversation about saving code.

  • I found that to get the webserver to work on esp8266, I had to delay starting the webserver for a few seconds after startup.

  • Could you help me Allobjects? Too much a newbie....Thanks!!

    var wifi = require('EspruinoWiFi'), WebServer = require('WebServer');

    function onInit() {

    if (wifi)
        wifi.startAP('Espruino_Server', {password:"aabbccddee"}, startServer);


    function startServer() {

    var webs = new WebServer({
        port: 8080,
        file_system: '/var/www/',
        memory: {
            'index.html': { 
                'type': 'text/html',
                'content': '<html><head><script src="index.js"></script></head><body>' +
                    '<p>Hello from in memory HTML!</p>' +
                    '<br><button onclick="hello()"> Run client side JavaScript</button>' +
                    '<br><br><button onclick="window.open(\'index.njs\', \'_top\')"> Run server side JavaScript</button>' +
                    '<br><br><button onclick="window.open(\'index.txt\', \'_top\')"> Go to a simple text</button>' +
                    '<br><p align="right"> <small>Espruino WebServer</small> </p>' +
            'index.txt': { 
                'content': 'Hello from in memory text!'
            'index.js': {
                'type': 'application/javascript',
                'content': "function hello(){ alert('Hello from in memory client side javascript'); }",
            'index.njs': {
                'content': index_njs
            'favicon.ico': {
                'type': 'image/x-icon',
                'content': "\0\0\x01\0\x01\0\x10\x10\x10\0\x01\0\x0­4\x00\xf0\0\0\0\x16\0\0\x00\x89PNG\x0d\x­0a\x1a\x0a\0\0\0\x0dIHDR\0\0\0\x10\0\0\0­\x10\x08\x06\0\0\0\x1f\xf3\xffa\0\0\x00\­xb7IDAT8\x8d\xa5S\xc1\x0d\x03!\x0csN\xb7­\x91w\xcaP\xde)3\xd1G\x09\x0a\x85\xab\xa­8\xea\x0f\x02\x82c\x1b0\x92x\x82\xbb\xb7­:\x8f\x08D\x84\xd5\xb5\x1b\x00H\xb6>N\x0­4uN\x12\x92\x10\x11S\xcd]\x0b\xbf\xa9\xe­9\x8a\x00\xa0I\x1a*\x06A\x97\xb7\x90\xd4­\x8e$A\x12\xee\xde\xb2vR\x90$\xc8q\xf6\x­03\xbc\x15Ldw]\x88zpc\xab*\x8c\x08H\xb2A­\x90\x1e\x97\xce\x1bd3\x00\xb8v\x9b\xa7p­\xf7\xb6\x10\x9cb\xc9\xe0Wd\x06\x17\x80v­\xe2\xfb\x09\x17\x00H\xfa\x8b\xc0\xba\x9­c\xe3CU\xf1\xc8@\xd2\x08fW\xf8i3?U\x12\x­18z\x16\xf5A\x9ddc_\xee\xbd~e{*z\x01|\xc­dnfT\x03\x0an\0\0\0\x00IEND\xaeB`\x82"
    webs.on('start', function (WebServer) {
        console.log('WebServer listening on port ' + WebServer.port);
    webs.on('request', function (request, response, parsedUrl, WebServer) {
        console.log('WebServer requested', parsedUrl);
    webs.on('error', function (error, WebServer) {
        console.log('WebServer error', error);


    function index_njs(req, res, uri, webs) {

    return {
        type: 'text/html',
        content: '<html>' + 
            '<p>Hello from in memory server side javascript!</p>' +
            '<p><b>Espruino Memory Usage: </b><br>' + JSON.stringify(process.memory()) + '</p>' + 
            '<p><b>Espruino Flash Usage: </b><br>' + JSON.stringify(require('Flash').getFree(­)) + '</p>' + 


  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

How to cleanup (purge) a socket connection object?

Posted by Avatar for ceremcem @ceremcem