'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 1:[1]

I don't think gremlin-javascript works well in a browser unfortunately. I assume that is the issue you are running into. Note that there already is an issue for this with TINKERPOP-2143 and an associated pull request that has been partially done now for a long time but has not yet been completed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stephen mallette