Examples
Serve Over HTTP
Serve your graph database over HTTP for remote access.
CogDB can serve your graph over HTTP, allowing remote clients to query it.
Starting the Server
from cog.torque import Graph
# Create and populate the graph
g = Graph("mydata")
g.put("hello", "msg", "world")
# Start the HTTP server
g.serve(port=8080)The server runs on the specified port and serves the graph at /graphname.
Connecting from a Client
From another machine or process, connect to the remote graph:
from cog.torque import Graph
# Connect to remote graph
remote = Graph.connect("http://192.168.1.5:8080/mydata")
# Query the remote graph (same API as local)
remote.v("hello").out("msg").all()
# {'result': [{'id': 'world'}]}Remote API
The remote graph supports the same query API as local graphs:
# All standard traversals work
remote.v("hello").out().all()
remote.v().has("msg", "world").all()
remote.v("hello").out("msg").count()Server Configuration
# Custom host and port
g.serve(host="0.0.0.0", port=9000)Public Graph Sharing
Share your graph over the internet using the CogDB relay service.
from cog.torque import Graph
# Create a knowledge graph
g = Graph("tech_stack")
g.put_batch([
("python", "used_by", "django"),
("python", "used_by", "flask"),
("javascript", "used_by", "react"),
("javascript", "used_by", "nextjs"),
("react", "maintained_by", "meta"),
])
# Share publicly via CogDB relay
g.serve(share=True)
# Get the public URL to share with others
print(g.share_url()) # https://abc123.s.cogdb.ioHow It Works
- When you call
serve(share=True), CogDB establishes a secure WebSocket connection to the CogDB relay - The relay provides a unique public URL (e.g.,
https://abc123.s.cogdb.io/) - Your graph remains on your machine — the relay only forwards HTTP requests
- Anyone with the URL can query the graph
Public links are accessible to anyone. By default, shared graphs are read-only. Use writable=True carefully if you need remote writes.
Connecting to a Shared Graph
Others can connect using just the public URL:
from cog.torque import Graph
# Connect from anywhere in the world
shared = Graph.connect("https://abc123.s.cogdb.io/tech_stack")
# Query the shared graph
shared.v("python").out("used_by").all()
# {'result': [{'id': 'django'}, {'id': 'flask'}]}Use Cases
- Microservices: Share graph data between services
- Distributed systems: Access graph from multiple machines
- Development: Query production data remotely
- APIs: Build REST APIs on top of graph queries