【发布时间】:2022-11-14 16:59:32
【问题描述】:
Memgrpah 是否支持通过 WebSocket 进行连接?我找不到执行此操作所需的最少代码。
【问题讨论】:
标签: websocket memgraphdb
Memgrpah 是否支持通过 WebSocket 进行连接?我找不到执行此操作所需的最少代码。
【问题讨论】:
标签: websocket memgraphdb
您只需要一个使用 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 找到更多信息。
【讨论】: