I have a typescript app that uses the antv/x6 version 2 to enable the user to draw diagrams. My app will allow users to collaborate on diagrams, so when a user makes a change in their instance in their session, the change will be serialized and forwarded to a server, and the server will distribute the change message to all the other clients that have sessions for the same diagram.
Because of this multi-user scenario, my app has to monitor all the changes to the current graph, and figure out whether a significant change occurred, and serialize and propagate the change.
AntV/X6 renders the graph and all the shapes in it as html and svg. There is an object model around this, and the graph and/or its objects can be serialized to json. The json is extensible, and I add an array field to the shape objects in the graph, allowing metadata key/value string pairs to be added to shapes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
When I try to wrap the shapes in classes and interact with them in a controlled way, e.g. shape.changePropertyX(newValue), I run into a lot of complexity and conflicts with AntV/X6, and I end up having to re-implement native AntV/X6 functionality. I also constantly struggle with using styles to control shape display attributes. The change monitoring actually works well though.
When I try to intercept events (I built an event bus class that centralizes event monitoring), I run into problems with AntV/X6 not receiving events accurately (mouse click events, etc.).
I initially thought it was a good design pattern to isolate the shapes and use an interface specific to my app to only allow graph interaction in controlled ways, and to also centralize change management and event handling, but I don't know if that is the right pattern given that I'm wrapping a very rich and complex library.
What I'm asking you to do is to present the pros and cons of any reasonable approaches to the architecture of my app's graphing functionality.
Assistant
Here's a balanced analysis of architectural approaches for your collaborative diagramming app with AntV/X6, considering your challenges with wrapping and event handling:
Approach 1: Direct AntV/X6 Event Handling (Minimal Abstraction)
Pros:
✅ Native compatibility: Leverage built-in events like node:changed, edge:connected, cell:removed
✅ Lower complexity: No abstraction conflicts or reimplementation needed
✅ Accurate change detection: Handle changes exactly where X6 emits them
✅ Better performance: Avoid wrapper overhead
Cons:
❗ Event filtering complexity: Must distinguish local vs. remote changes to prevent echo loops
❗ Granularity challenges: Some changes require diffing snapshots (e.g., graph:changed)
❗ No centralized control: Business logic scattered across event handlers
Implementation Tip:
// Listen to native events with change tracking
graph.on('node:changed', ({ node, options }) => {
if (options?.remote) return; // Ignore remote changes
const delta = getChangeDelta(node); // Your diff logic
server.sendChange(delta);
});
// Applying remote changes
function applyRemoteChange(delta) {
const node = graph.getCellById(delta.id);
node.prop({ ...delta.props, remote: true }); // Flag prevents echo
}
Approach 2: State Mirroring (Single Source of Truth)
Pros:
✅ Change detection simplicity: Compare diagram state before/after mutations
✅ Decoupled collaboration: Serialization/network logic separate from X6
✅ Undo/redo readiness: Natural fit for state snapshots
✅ Framework-agnostic: Core logic independent of X6
Cons:
❗ Synchronization overhead: Must map state ↔ X6 continuously
❗ Performance concerns: Full diffs on large diagrams may lag
❗ Double maintenance: Shape logic exists in both state and X6
Implementation Tip:
// Pseudo-code state flow
userInteraction → stateManager.update(modelDelta) → {
1. Apply to local state
2. Sync to X6: graph.getCell(id).prop(delta)
3. Broadcast to server: server.send(delta)
}
// Intercept changes without wrapping
graph.on('node:change:data', ({ node, current }) => {
if (current.metaRef) validateMetadata(nodeMetadata.get(current.metaRef));
});
Benchmark performance - For large diagrams, snapshot diffing may become expensive. Prioritize incremental change events where possible.
Why this works: This hybrid approach respects X6's internal architecture while providing the control needed for collaboration. By letting X6 manage rendering and low-level interactions, but keeping business state separate, you avoid fighting the library while maintaining clean serialization pathways.