Search code examples
javascriptorientdborientjs

connection timeout OrientDB with Javascript API


In my webapp client side script I'm using the OrientDB Javascript API (orientdb-api.js). When the script initializes I run this code:

var orientdb = new ODatabase("http://localhost:2480/testapp");
var orientdbinfo = orientdb.open('root', 'admin');

This works fine and I can do all the various queries etc, as long as I don't wait more than 15 seconds between them. If I do, I get "error 401 (Unauthorised)" returned.

I know for a fact that this is the socket connection timing out. The timeframe matches the 15000ms timeout setting in the config. Also, as a test I've built a little button that calls the orientdb.open method above and reopens the connection. After I hit that button I can access the DB again.

Currently the queries and commands are being called directly in my script as I trigger actions from my web UI. Am I just being lazy and am I actually supposed to wrap every query in a function that tests the connection first and re-initializes if it is closed, or is there something I'm missing? If the former, what is an elegant way of coding that? If the latter, what am I missing?

To get around this I'm running a setInterval function that opens a new socket every 14 seconds. That will get me through my testing for sure, but I realise it's a hack.


Solution

  • When you start the OrientDB server, it creates two sockets: 2424 (binary) and 2480 (HTTP).

    Because OrientJS uses the binary protocol, you need to connect to port 2424.

    Try:

    var orientdb = new ODatabase("http://localhost:2424/testapp");
    var orientdbinfo = orientdb.open('root', 'admin');
    

    And the socket should stay open (longer).