Build real-time applications with WebSockets for Google Antigravity IDE
# WebSocket Real-time Guide for Google Antigravity
Master real-time communication with WebSockets in Google Antigravity IDE. This comprehensive guide covers connection management, message protocols, reconnection strategies, scaling patterns, and security considerations for building responsive real-time applications.
## Configuration
Configure your Antigravity environment for WebSockets:
```typescript
// .antigravity/websocket.ts
export const wsConfig = {
features: {
heartbeat: true,
reconnection: true,
compression: true,
rooms: true
},
protocols: ["json", "binary"]
};
```
## Server Implementation
Create a WebSocket server:
```typescript
import { WebSocketServer, WebSocket } from "ws";
const wss = new WebSocketServer({ port: 8080 });
interface Client extends WebSocket {
id: string;
userId: string;
rooms: Set<string>;
}
const clients = new Map<string, Client>();
wss.on("connection", (ws: Client) => {
ws.id = crypto.randomUUID();
ws.rooms = new Set();
clients.set(ws.id, ws);
ws.on("message", (data) => {
const message = JSON.parse(data.toString());
handleMessage(ws, message);
});
ws.on("close", () => {
clients.delete(ws.id);
});
});
```
## Room Management
Implement room-based messaging:
```typescript
function joinRoom(client: Client, roomName: string) {
client.rooms.add(roomName);
broadcastToRoom(roomName, {
type: "user_joined",
userId: client.userId
}, client.id);
}
function broadcastToRoom(roomName: string, data: unknown, excludeId?: string) {
clients.forEach((client) => {
if (client.rooms.has(roomName) && client.id !== excludeId) {
client.send(JSON.stringify(data));
}
});
}
```
## Best Practices
Follow these guidelines for WebSockets:
1. **Implement heartbeats** - Detect stale connections
2. **Handle reconnection** - Exponential backoff
3. **Queue messages** - Buffer during disconnects
4. **Use rooms** - Efficient broadcast targeting
5. **Authenticate connections** - Verify on upgrade
6. **Rate limit** - Prevent message flooding
Google Antigravity IDE provides intelligent WebSocket scaffolding and real-time pattern suggestions.This WebSocket prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their websocket implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For WebSocket projects, consider mentioning your framework version, coding style, and any specific libraries you're using.