Querying in a JavaScript browser app


On this page

    Comunica can run in both Node.js JavaScript applications, and as client-side applications in Web browsers.

    1. Using a pre-built version

    The easiest way to use Comunica in your Web app, is by using a pre-built Comunica SPARQL version that is served via a GitHub CDN:

    <script src="https://rdf.js.org/comunica-browser/versions/v2/engines/query-sparql/comunica-browser.js"></script>
    <script language="JavaScript">
      new Comunica.QueryEngine().queryBindings(`
      SELECT * {
        ?s ?p <http://dbpedia.org/resource/Belgium>.
        ?s ?p ?o
      } LIMIT 100
    `, {
      sources: ['https://fragments.dbpedia.org/2015/en'],
    }).then(function (bindingsStream) {
      bindingsStream.on('data', function (data) {
        // Each variable binding is an RDFJS term
        console.log(data.get('s').value + ' ' + data.get('p').value + ' ' + data.get('o').value);
      });
    });
    </script>
    
    The code example above will always make use of the the latest Comunica version in the 2.x.x range. Instead, you can use a specific version.

    The full API of Comunica is available under the Comunica namespace. More information on its usage can be found in the guide on using Comunica in a JavaScript app.

    2. Bundling for the browser

    Comunica is compatible with browser bundler tools such as Webpack and browserify. If you are not familiar with these tools, you can read the following guides:

    You will need to create a "UMD bundle" and supply a name (e.g. with the -s Comunica option in browserify).

    Refer to our specific guide on building for the browser if you want to build specific configurations of Comunica for the browser.