Ably batch lock exmple
HTML
<script src="https://cdn.ably.com/lib/ably.min-2.js"></script>
<script src="https://cdn.ably.com/spaces/0.4.0/iife/index.bundle.js"></script>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ably Spaces Lock Test</title>
</head>
<body>
<h1>Ably Spaces Lock Test</h1>
<div class="controls">
<button id="connect">Connect to Space</button>
<button id="acquire-locks">Acquire All Locks</button>
<button id="disconnect">Disconnect</button>
</div>
<div class="container">
<h2>Document Sections (50 total)</h2>
<div class="sections-container" id="sections-container">
<!-- Sections will be generated by JavaScript -->
</div>
</div>
<div class="status">
<h3>Status Log</h3>
<div id="log"></div>
</div>
</body>
</html>
CSS
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
.container {
border: 1px solid #ddd;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
}
.sections-container {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 10px;
}
.section {
border: 1px solid #ddd;
padding: 8px;
border-radius: 3px;
font-size: 12px;
height: 60px;
overflow: hidden;
}
.section h3 {
margin: 0 0 5px 0;
font-size: 14px;
}
.section p {
margin: 0;
}
.section.locked {
background-color: #f8f9fa;
border-color: #28a745;
}
.controls {
margin-bottom: 20px;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #0069d9;
}
.status {
margin-top: 20px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 3px;
height: 200px;
overflow-y: auto;
background-color: #f8f9fa;
}
.log-entry {
margin-bottom: 5px;
font-family: monospace;
}
.error {
color: #dc3545;
}
.success {
color: #28a745;
}
JavaScript
// Configuration
const API_KEY = "TEST_KEY" // Replace with your Ably API key
const SPACE_NAME = "test-space"
const CLIENT_ID = "user-" + Math.floor(Math.random() * 1000)
// Elements
const connectBtn = document.getElementById("connect")
const acquireLocksBtn = document.getElementById("acquire-locks")
const disconnectBtn = document.getElementById("disconnect")
const logElement = document.getElementById("log")
// Lock IDs and attributes
const lockIds = Array.from({ length: 50 }, (_, i) => `section${i + 1}`)
const lockAttributes = {}
// Generate attributes for each section
lockIds.forEach((id, index) => {
const types = [
"header",
"paragraph",
"footer",
"sidebar",
"image",
"list",
"table",
"code",
"quote",
"button",
]
lockAttributes[id] = {
type: types[index % types.length],
index: index + 1,
}
})
// Variables
let client = null
let spaces = null
let space = null
let acquiredLocks = []
let connected = false
// Functions
function log(message, type = "") {
const entry = document.createElement("div")
entry.className = `log-entry ${type}`
entry.textContent = `${new Date().toLocaleTimeString()}: ${message}`
logElement.appendChild(entry)
logElement.scrollTop = logElement.scrollHeight
}
function updateSectionUI() {
// Reset all sections
lockIds.forEach((id) => {
const element = document.getElementById(id)
element.classList.remove("locked")
})
// Highlight locked sections
acquiredLocks.forEach((lock) => {
const element = document.getElementById(lock.id)
if (element) {
element.classList.add("locked")
}
})
}
async function connectToSpace() {
try {
log("Initializing Ably client...")
client = new Ably.Realtime({ key: API_KEY, clientId: CLIENT_ID })
log("Creating Spaces instance...")
spaces = new Spaces(client)
log(`Connecting to space: ${SPACE_NAME}`)
space = await spaces.get(SPACE_NAME)
log("Entering space...")
await space.enter({...