我可以使用WebSocket连接到Memgraph吗?

vwkv1x7d  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(114)

Memgrpah支持通过WebSocket连接吗?我找不到最少的代码来完成。

eqfvzcg8

eqfvzcg81#

你所需要的只是一个使用WebSocket连接Memgraph的客户端,Memgraph会自动识别连接的性质,你要连接的端口保持不变。
您应该使用Memgraph的地址和配置标志--bolt-port定义的端口号连接到Memgraph(7687是默认端口)。
要通过WebSocket连接到memgraph,您可以使用JavaScript客户端。连接所需的最少代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Javascript Browser Example | Memgraph</title>
    <script src="https://cdn.jsdelivr.net/npm/neo4j-driver"></script>
  </head>
  <body>
    <p>Check console for Cypher query outputs...</p>
    <script>
      const driver = neo4j.driver(
        "bolt://localhost:7687",
        neo4j.auth.basic("", "")
      );

      (async function main() {
        const session = driver.session();

        try {
          await session.run("MATCH (n) DETACH DELETE n;");
          console.log("Database cleared.");

          await session.run("CREATE (alice:Person {name: 'Alice', age: 22});");
          console.log("Record created.");

          const result = await session.run("MATCH (n) RETURN n;");

          console.log("Record matched.");
          const alice = result.records[0].get("n");
          const label = alice.labels[0];
          const name = alice.properties["name"];
          const age = alice.properties["age"];

          if (label != "Person" || name != "Alice" || age != 22) {
            console.error("Data doesn't match.");
          }

          console.log("Label: " + label);
          console.log("Name: " + name);
          console.log("Age: " + age);
        } catch (error) {
          console.error(error);
        } finally {
          session.close();
        }

        driver.close();
      })();
    </script>
  </body>
</html>

您可以在Memgraph documentation site上找到更多信息。

相关问题