Google Sheets: Add WebSocket Support for Real-Time Communication Between Code.gs and Client-Side HTML
@vinay-google đang làm issue này rồi.
Từ ngày 28/8/2025.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Problem Description
Google Apps Script currently lacks support for persistent, bidirectional communication between server-side Code.gs and client-side HTML served via HtmlService. This forces developers to use inefficient polling mechanisms to achieve pseudo-real-time updates.
Current Limitations
google.script.runonly supports one-way RPC calls from client to server- No WebSocket or Server-Sent Events (SSE) support
- Polling with
setIntervalcreates unnecessary server load and latency - 6-minute execution limit prevents long-running server processes
Use Cases Impacted
- Real-time collaboration tools - Displaying live cursor positions or selection changes
- Dashboard applications - Live data updates without page refresh
- Progress indicators - Streaming progress of long-running operations
- Chat/notification systems - Instant message delivery within Sheets add-ons
Proposed Solution
Implement WebSocket support in Google Apps Script with the following API:
// Code.gs - Server-side
function onWebSocketConnection(ws) {
// Handle new WebSocket connection
ws.on('message', (data) => {
// Process incoming message
const parsed = JSON.parse(data);
if (parsed.type === 'subscribe') {
// Set up sheet listeners
const listener = SpreadsheetApp.onSelectionChange((e) => {
ws.send(JSON.stringify({
type: 'selectionUpdate',
range: e.range.getA1Notation(),
sheet: e.range.getSheet().getName(),
timestamp: new Date().getTime()
}));
});
ws.on('close', () => {
listener.remove(); // Clean up
});
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
}
// Alternative: SSE for one-way streaming
function* onServerSentEvent() {
const cache = CacheService.getUserCache();
while (true) {
const data = cache.get('latestUpdate');
if (data) {
yield data;
cache.remove('latestUpdate');
}
Utilities.sleep(100); // Non-blocking in actual implementation
}
}
// index.html - Client-side
<script>
// WebSocket approach
const ws = google.script.websocket.connect();
ws.onopen = () => {
ws.send(JSON.stringify({ type: 'subscribe' }));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
updateDisplay(data);
};
// Alternative: EventSource for SSE
const eventSource = google.script.eventSource.connect();
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
updateDisplay(data);
};
</script>
Benefits
- Reduced server load - Eliminate constant polling requests
- Lower latency - Instant updates instead of polling intervals
- Better UX - True real-time features in Sheets add-ons
- Resource efficiency - Maintain single connection vs repeated HTTP calls
- Cleaner code - Event-driven instead of interval-based patterns
Backward Compatibility
- Keep existing
google.script.runAPI unchanged - WebSocket/SSE features would be opt-in via new APIs
- Graceful fallback to polling if WebSocket unavailable
Security Considerations
- Maintain same OAuth scopes and permissions model
- WebSocket connections authenticated via existing session
- Rate limiting to prevent abuse
- Automatic timeout after inactivity period
- Ngôn ngữ chính
- JavaScript
- Star
- 5.2k
- Fork
- 2k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của googleworkspace/apps-script-samples
-
123Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 1/100
-
مدرسةĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
-
SchoolĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
-
Alvin Litana MirasolĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 1/100
googleworkspace/apps-script-samples#659 · 1 bình luận · 1 reaction ·
-
Password resetĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
googleworkspace/apps-script-samples#654 · 2 reaction ·
Tất cả issue của googleworkspace/apps-script-samples
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
bigskysoftware/htmx#4147 ·
Maintainer thường phản hồi trong vòng 5 ngày
-
Please add PNG images as wellĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
vshymanskyy/StandWithUkraine#316 ·
-
README: the green and red points are swapped (green = .fset tracking, red = .fset3 detection)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
bug/1-unconfirmed kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày