World! program that connects to the Memgraph database and executes simple
Running queries directly from a web browser is not a recommended usage because of additional requirements and possible performance issues. In other words, we encourage users to use server-side libraries and clients for top performance whenever possible.
To follow this guide, you will need:
- A running Memgraph instance. If you need to set up Memgraph, take a look at the Installation guide.
- A basic understanding of graph databases and the property graph model.
Memgraph doesn't have integrated support for
WebSocket which is required
during the execution of Cypher commands in any web browser. If you want to run
Cypher queries from a web browser,
websockify has to be up and running.
Requests from web browsers are wrapped into
WebSocket messages, and a proxy is
needed to handle the overhead. The proxy has to be configured so that the web
browser driver sends requests to the proxy port which sends them to Memgraph's
Bolt port. Presented with Cypher language, the communication goes like this:
Websockify runs in unencrypted HTTP (ws://) mode by default which isn't
encrypted, so to match that, Memgraph needs to be running with encryption turned
off (the default setting).
The code snippet below outlines a basic usage example that executes a couple of elementary queries. The first two steps are about starting Websockify to proxy queries to the database.
Let's jump in and connect a simple program to Memgraph.
1. Create a new directory for your application, for example
position yourself in it.
2. Create a
websockify.sh script with the
#!/bin/bash if [ ! -d "websockify-js" ]; then git clone https://github.com/novnc/websockify-js.gitficd websockify-js/websockifynpm install./websockify.js 9999 :7687
Websockify with the command:
4. To make the actual program, create a
program.html file and add the
5. Open the
program.html file in your browser and look for the output in
You should see an output similar to the following:
Database cleared.Record created.Record matched.Label: PersonName: AliceAge: 22
For real-world examples of how to use Memgraph, we suggest you take a look at the Tutorials page. You can also browse through the Database functionalities section to get an overview of all the functionalities Memgraph offers.