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="http://rdf.js.org/comunica-browser/versions/1/packages/actor-init-sparql/comunica-browser.js"></script>
    <script language="JavaScript">
      Comunica.newEngine().query(`
      SELECT * {
        ?s ?p <http://dbpedia.org/resource/Belgium>.
        ?s ?p ?o
      } LIMIT 100
    `, {
      sources: ['http://fragments.dbpedia.org/2015/en'],
    }).then(function (result) {
      result.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 1.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.