Issue with websocket connection to Moralis Server

Hi there,

Since speedy nodes are deprecated, I’m currently using LiveQuery to listen to new events (performed smart contract event syncs before). However, sometimes I’m facing this issue when starting:

Error: Unexpected server response: 503
    at ClientRequest.<anonymous> (/root/project/node_modules/ws/lib/websocket.js:604:7)
    at ClientRequest.emit (events.js:314:20)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:601:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:122:17)
    at TLSSocket.socketOnData (_http_client.js:474:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on WebSocket instance at:
    at abortHandshake (/root/project/node_modules/ws/lib/websocket.js:731:15)
    at ClientRequest.<anonymous> (/root/project/node_modules/ws/lib/websocket.js:604:7)
    [... lines matching original stack trace ...]
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
(node:11285) UnhandledPromiseRejectionWarning: Error: Received an error with invalid JSON from Parse: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>

Can you please tell me how to fix? Thank you
FYI, I use Moralis Node with serverUrl, appId and masterKey opts.

Hi, I don’t know what is the code that you tried, you should be able to use a live query, or to connect directly to mongo db too if you want.

We are working now on an API specific for event syncs that could help you.

Hi,

Actually, LiveQuery works fine when I run the script on my local computer. However, I don’t have any idea why sometimes I open the script on another server, it got the mentioned error.

This is a bunch of my code:

const Moralis = require('moralis/node');
const moralisConfig = require('./.moralis.json');

const startConnection() = async () => {
    await Moralis.start(moralisConfig);
    const query = new Moralis.Query("Transfer");
    const subscription = await query.subscribe();
    subscription.on('update', async (object) => {
        console.log(object);
    });
};

startConnection();

Currently, I could hotfix it by listening to error event and retry to connect to the server, but I am still looking for a better solution.

BTW, can’t wait to see your event syncs API. Good work guys.

Maybe you could subscribe to more events to see if you get more information about what happens. But it looks like you get another type of error.