Search code examples
javascriptnode.jsazure-cosmosdbgremlin

Simple connectivity to a Gremlin DB via a Javascript Appliaction


I apologies for such a basic question, but I can't seem to figure out how to do this and the docs are all very specific.

I'm just trying to connect to a standard Gremlin DB (Cosmos) using gremlin. It works great from the server, but when I connect from the browser, I get this error:

Error: ws does not work in the browser. Browser clients must use the native WebSocket object

Nothing terribly complicated, and the error seems to be pretty clear.

Here is the code:

     constructor() {
        var authenticator = new Gremlin.driver.auth.PlainTextSaslAuthenticator(`/dbs/${config.database}/colls/${config.collection}`, config.primaryKey);
        this.gremlin_config_options = {
            authenticator,
            traversalsource: "g",
            rejectUnauthorized: true,
            mimeType: "application/vnd.gremlin-v2.0+json"
        }

       var DriverRemoteConnection = Gremlin.driver.DriverRemoteConnection;
        this.Graph = Gremlin.structure.Graph;

        var dc = new DriverRemoteConnection(this.gremlin_websocket,this.gremlin_config_options);

        this.graph = new this.Graph();
        this.g = this.graph.traversal().withRemote(this.gremlin_websocket);
      };

When I do the following code, it completes without the Javascript error.

    constructor() {
        var authenticator = new Gremlin.driver.auth.PlainTextSaslAuthenticator(`/dbs/${config.database}/colls/${config.collection}`, config.primaryKey);
        this.gremlin_config_options = {
            authenticator,
            traversalsource: "g",
            rejectUnauthorized: true,
            mimeType: "application/vnd.gremlin-v2.0+json"
        }

        this.Graph = Gremlin.structure.Graph;

        this.gremlin_websocket = new WebSocket('ws://test_db.gremlin.cosmos.azure.com:8182/')

        this.graph = new this.Graph();
        this.g = this.graph.traversal().withRemote(this.gremlin_websocket);
      };

However, I need to pass along authentication and collection information (currently in the authenticator object). But WebSocket doesn't seem to support it, and Driver Remote Connection doesn't seem to natively take the websocket. What should I do?


Solution

  • Check out gremlin-browser, a JavaScript Gremlin Language Variant for the Browser based on version gremlin-js v3.5.2 and isomorphic-ws v4.0.1. Tested using Azure Cosmos DB Gremlin API 2022 :)