hapijs / nes

WebSocket adapter plugin for hapi routes
Other
502 stars 87 forks source link

Error: read ECONNRESET #244

Closed SamuelEarl closed 6 years ago

SamuelEarl commented 6 years ago

I am trying to use a publication/subscription model and I am seeing some errors that I can't debug. I am pretty sure they are related to the client closing the WebSocket connection, but I don't know how to fix the problem.

Here is my code:

server.js:

// Publish "/collections-array"
(async () => {
  try {
    server.subscription('/collections-array');
    server.publish('/collections-array', 'Message from the server');
  }
  catch (err) {
    console.log(err);
  }
})();

client.js:

const client = new Nes.Client('ws://localhost:8080');

// Subscribe to "/collections-array" publication
try {
  (async () => {
    await client.connect();
    const handleUpdates = (update) => {
      console.log(update);
    };
    client.subscribe('/collections-array', handleUpdates);
  })();
}
catch (err) {
  console.log(err);
}

The strange thing is that I am successfully using pubs/subs in other parts of my app, but this particular instance throws weird errors. For example, I tried using the server.broadcast() pattern and I would see "Message from the server" in my browser console on occasion, but most of the time I wouldn't see anything. After switching to the pub/sub pattern from the above code snippets, I haven't seen any message from the server in my browser console.

I have seen this error in my server's terminal window at random times, but not consistently:

Error: read ECONNRESET
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:628:25)

I tried the following code and it gave an Uncaught (in promise) Error: Cannot connect while client attempts to reconnect error in the browser console:

server.js:

(async () => {
  try {
    // Publish "/collections-array"
    server.subscription('/collections-array');
    server.publish('/collections-array', 'Message from the server');

    // API endpoint to publish data from the server
    server.route({
      method: 'GET',
      path: '/api/collections-array',
      options: {
        id: 'collectionsArray',
        async handler(request) {
          try {
            return "API Endpoint";
          }
          catch (err) {
            console.log(err);
          }
        },
      },
    });
  }
  catch (err) {
    console.log(err);
  }
})();

client.js:

const client = new Nes.Client('ws://localhost:8080');

// Subscribe to "/collections-array" publication
try {
  (async () => {
    await client.connect();
    const handleUpdates = (update) => {
      console.log(update);
    };
    client.subscribe('/collections-array', handleUpdates);
  })();
}
catch (err) {
  console.log(err);
}

// Invoke the "collectionsArray" route
try {
  (async () => {
    await client.connect();
    const { payload } = await client.request('collectionsArray');
    console.log(payload);
  })();
}
catch (err) {
  console.log(err);
}

If I comment out the block that is documented with // Subscribe to "/collections-array" publication, then I will see "API Endpoint" in the browser console.

I read that I just have to handle the ECONNRESET error and it should fix the problem, but I thought I was already doing that with the try/catch blocks.

Any ideas as to what is going on?

Thank you!

heldercruvinel commented 6 years ago

Hi, have you fixed the issue? I have the situation.

ErickWendel commented 6 years ago

@heldercruvinel it's the same situation with the same example ? If do you have another example send to us and I'll try to check :D

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.