Skip to content

Commit 2ee08c4

Browse files
feat: add word addin example (#757)
1 parent 5056aed commit 2ee08c4

38 files changed

Lines changed: 4129 additions & 1 deletion

β€Ž.gitignoreβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ dist
88
dist-ssr
99
*.local
1010
.env
11-
.env.*
1211
.npmrc
1312
.eslintcache
1413

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
auth0-config.js
2+
server-domain.js
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# SuperDoc MS Add-in Sync
2+
3+
Real-time document synchronization between Microsoft Word Add-in and web editor using WebSocket communication.
4+
5+
## Architecture
6+
7+
The system consists of three main components:
8+
- **MS Word Add-in** (`src/taskpane/taskpane.js`) - Runs inside Microsoft Word
9+
- **Web Editor** (`server/public/editor.js`) - Browser-based document editor
10+
- **Node.js Server** (`server/server.js`) - WebSocket server handling real-time sync
11+
12+
## WebSocket Events
13+
14+
The WebSocket communication uses the following event types:
15+
16+
### `client_ready`
17+
**Sent by:** Web Editor
18+
**Handled by:** Server (broadcasts to other clients)
19+
**Purpose:** Signals that a browser client has loaded and is ready to receive authentication
20+
21+
```javascript
22+
// Sent by editor.js
23+
websocket.send(JSON.stringify({
24+
type: 'client_ready'
25+
}));
26+
27+
// Broadcasted by server.js to other clients
28+
{
29+
type: 'client_ready',
30+
timestamp: '2024-01-01T00:00:00.000Z'
31+
}
32+
```
33+
34+
### `token_transfer`
35+
**Sent by:** MS Word Add-in
36+
**Handled by:** Server (validates and broadcasts to other clients), Web Editor
37+
**Purpose:** Transfers authentication token from Word add-in to web editor
38+
39+
```javascript
40+
// Sent by taskpane.js
41+
websocket.send(JSON.stringify({
42+
type: 'token_transfer',
43+
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik...'
44+
}));
45+
46+
// Broadcasted by server.js after validation
47+
{
48+
type: 'token_transfer',
49+
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik...',
50+
user: {
51+
email: 'user@example.com',
52+
name: 'User Name',
53+
picture: 'https://example.com/avatar.jpg'
54+
},
55+
timestamp: '2024-01-01T00:00:00.000Z'
56+
}
57+
58+
// Error response
59+
{
60+
type: 'token_transfer',
61+
error: 'Invalid token',
62+
timestamp: '2024-01-01T00:00:00.000Z'
63+
}
64+
```
65+
66+
### `document_update`
67+
**Sent by:** MS Word Add-in, Web Editor
68+
**Handled by:** Server (validates and broadcasts to other clients), MS Word Add-in, Web Editor
69+
**Purpose:** Real-time document synchronization between clients
70+
71+
```javascript
72+
// Sent by taskpane.js or editor.js
73+
websocket.send(JSON.stringify({
74+
type: 'document_update',
75+
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik...',
76+
document: 'UEsDBBQABgAIAAAAIQDb4fbL7...' // Base64 encoded .docx
77+
}));
78+
79+
// Broadcasted by server.js after validation
80+
{
81+
type: 'document_update',
82+
document: 'UEsDBBQABgAIAAAAIQDb4fbL7...',
83+
author: 'user@example.com',
84+
timestamp: '2024-01-01T00:00:00.000Z'
85+
}
86+
```
87+
88+
### `close`
89+
**Sent by:** Server
90+
**Handled by:** MS Word Add-in, Web Editor
91+
**Purpose:** Notifies clients when another connection closes
92+
93+
```javascript
94+
// Broadcasted by server.js
95+
{
96+
type: 'close',
97+
timestamp: '2024-01-01T00:00:00.000Z'
98+
}
99+
```
100+
101+
### `error`
102+
**Sent by:** Server
103+
**Handled by:** MS Word Add-in, Web Editor
104+
**Purpose:** Notifies clients when a connection error occurs
105+
106+
```javascript
107+
// Broadcasted by server.js
108+
{
109+
type: 'error',
110+
timestamp: '2024-01-01T00:00:00.000Z'
111+
}
112+
```
113+
114+
## Authentication Flow
115+
116+
1. Web editor loads and sends `client_ready` to server
117+
2. Server broadcasts `client_ready` to Word add-in
118+
3. Word add-in sends `token_transfer` with user's authentication token
119+
4. Server validates token against Auth0 and broadcasts `token_transfer` with user info to web editor
120+
5. Both clients are now authenticated and can send `document_update` events
121+
122+
## Real-time Synchronization
123+
124+
- Document changes in Word trigger `document_update` events via selection change detection
125+
- Document changes in web editor trigger `document_update` events via SuperDoc's `onEditorUpdate` callback
126+
- All `document_update` events include the full document as Base64-encoded .docx
127+
- Server validates authentication token before broadcasting updates
128+
- Clients update their document content when receiving `document_update` events
129+
130+
## Setup
131+
132+
1. Install dependencies:
133+
```bash
134+
npm install
135+
cd server && npm install
136+
```
137+
138+
2. Configure environment variables:
139+
140+
**Auth0 Configuration** - Set up `src/auth0-config.js`:
141+
- Get your Auth0 domain, client ID, and audience from your [Auth0 Dashboard](https://manage.auth0.com/)
142+
- Create a new application or use an existing Single Page Application
143+
- Configure the redirect URLs to include your add-in's domain
144+
145+
**Server Configuration** - Set up `server/.env`:
146+
- `AUTH0_DOMAIN`: Your Auth0 domain (e.g., `yourapp.us.auth0.com`)
147+
- `AUTH0_AUDIENCE`: Your Auth0 API identifier
148+
- Any additional environment variables required by your cloud function
149+
150+
These environment variables are required for:
151+
- **Auth0**: Authenticating users and validating JWT tokens
152+
- **Cloud Function**: Server-side token validation and WebSocket communication
153+
154+
You can reference the example files in the same directories.
155+
156+
3. Start the server:
157+
```bash
158+
npm run server
159+
```
160+
161+
4. Build and run the add-in:
162+
```bash
163+
npm run build
164+
npm start
165+
```
108 KB
Loading
108 KB
Loading
114 KB
Loading
114 KB
Loading
114 KB
Loading
114 KB
Loading

0 commit comments

Comments
Β (0)