-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: realtime manager #1982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
satya-nutella
wants to merge
16
commits into
onlook-dev:main
Choose a base branch
from
satya-nutella:feat/realtime-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: realtime manager #1982
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b77e1f4
feat: add row-level security policies and access functions for projec…
satya-nutella a2dd36a
feat: add db:migrate command and Supabase migration workflow
satya-nutella a6456b8
chore: rls changes
satya-nutella a9235c6
fix: update triggers and logic for conversations and messages
satya-nutella 003dea5
chore: minor fixes
satya-nutella 98d6f8b
fix: typo in elseif
satya-nutella 27691d3
feat: add invitation handling and layout components
satya-nutella 3a7049f
feat: enhance invitation acceptance flow
satya-nutella dd64f4d
fix: await render in dryRun for invitation email
satya-nutella 948f8c1
feat: add feature flags for collaboration
satya-nutella 2ab8be8
feat: integrate realtime collaboration features
satya-nutella 66c8416
merge from main
Kitenite 6f29cf6
fix main conflict
Kitenite f9e68cc
Merge branch 'main' of https://github.com/onlook-dev/onlook into feat…
satya-nutella a89919a
chore: remove console log
satya-nutella 182afa5
Merge branch 'feat/realtime-manager' of https://github.com/satya-nute…
satya-nutella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/web/client/src/components/store/editor/realtime/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { RealtimeManager } from './manager'; | ||
import { projectManager } from '../../project'; | ||
import { userManager } from '../../user'; | ||
|
||
export const realtimeManager = new RealtimeManager(projectManager, userManager); | ||
const RealtimeContext = createContext(realtimeManager); | ||
export const useRealtimeManager = () => useContext(RealtimeContext); |
79 changes: 79 additions & 0 deletions
79
apps/web/client/src/components/store/editor/realtime/manager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { makeAutoObservable } from 'mobx'; | ||
import { RealtimeChannel } from '@supabase/supabase-js'; | ||
import { ProjectManager } from '../../project/manager'; | ||
import { createClient } from '@/utils/supabase/client'; | ||
import type { RealtimeEvent, RealtimeUser } from '@onlook/models'; | ||
import type { UserManager } from '../../user/manager'; | ||
|
||
export class RealtimeManager { | ||
readonly supabase = createClient(); | ||
private _channel: RealtimeChannel | null = null; | ||
private _users: Record<string, RealtimeUser> = {}; | ||
private _isSubscribed = false; | ||
|
||
constructor( | ||
private projectManager: ProjectManager, | ||
private userManager: UserManager, | ||
) { | ||
makeAutoObservable(this); | ||
|
||
this.init(); | ||
} | ||
|
||
private init() { | ||
if (!this.projectManager.project?.id) { | ||
console.error('Project ID is not set'); | ||
return; | ||
} | ||
|
||
// Clean up any existing subscriptions | ||
this.clean(); | ||
|
||
this._channel = this.supabase.channel(this.projectManager.project.id); | ||
this._channel | ||
.on('broadcast', { event: '*' }, (payload) => { | ||
//console.log('payload', payload); | ||
}) | ||
.subscribe((status) => { | ||
if (status === 'SUBSCRIBED') { | ||
this._isSubscribed = true; | ||
} | ||
}); | ||
} | ||
|
||
private add(user: RealtimeUser) { | ||
this._users[user.id] = user; | ||
} | ||
|
||
private remove(id: string) { | ||
delete this._users[id]; | ||
} | ||
|
||
private update(user: RealtimeUser) { | ||
this._users[user.id] = user; | ||
} | ||
|
||
private getUsers() { | ||
return this._users; | ||
} | ||
|
||
clean() { | ||
this._channel?.unsubscribe(); | ||
this._channel = null; | ||
this._users = {}; | ||
this._isSubscribed = false; | ||
} | ||
|
||
send(event: RealtimeEvent) { | ||
if (!this._isSubscribed || !this._channel) { | ||
console.error('Not subscribed to realtime channel'); | ||
return; | ||
} | ||
|
||
this._channel.send({ | ||
type: 'broadcast', | ||
event: event.event, | ||
payload: event, | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { UserMetadata } from '../user'; | ||
import type { Frame } from '../project'; | ||
|
||
export interface RealtimeUser extends UserMetadata { | ||
position: { | ||
x: number; | ||
y: number; | ||
}; | ||
} | ||
|
||
export enum RealtimeEventType { | ||
USER_ADDED = 'user_added', | ||
USER_REMOVED = 'user_removed', | ||
USER_UPDATED = 'user_updated', | ||
FRAME_UPDATED = 'frame_updated', | ||
FRAME_DELETED = 'frame_deleted', | ||
FRAME_CREATED = 'frame_created', | ||
} | ||
|
||
export interface FrameCreatedEvent { | ||
event: RealtimeEventType.FRAME_CREATED; | ||
payload: Frame; | ||
} | ||
|
||
export interface FrameUpdatedEvent { | ||
event: RealtimeEventType.FRAME_UPDATED; | ||
payload: Frame; | ||
} | ||
|
||
export interface FrameDeletedEvent { | ||
event: RealtimeEventType.FRAME_DELETED; | ||
payload: Frame; | ||
} | ||
|
||
export interface UserAddedEvent { | ||
event: RealtimeEventType.USER_ADDED; | ||
payload: RealtimeUser; | ||
} | ||
|
||
export interface UserUpdatedEvent { | ||
event: RealtimeEventType.USER_UPDATED; | ||
payload: RealtimeUser; | ||
} | ||
|
||
export type RealtimeEvent = | ||
| FrameCreatedEvent | ||
| FrameUpdatedEvent | ||
| FrameDeletedEvent | ||
| UserAddedEvent | ||
| UserUpdatedEvent; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The broadcast event handler currently doesn't process incoming events - it only logs them to the console (which is commented out). For the realtime functionality to work properly, this handler should parse the payload and update the internal state by calling the appropriate methods (
add
,update
,remove
) based on the event type. Without this implementation, the manager will receive events but won't update its state, making the realtime features ineffective.Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.