Skip to content

Commit

Permalink
Merge pull request #1685 from appwrite/fix-realtime
Browse files Browse the repository at this point in the history
Fix realtime on multi-region for backups
  • Loading branch information
ItzNotABug authored Feb 20, 2025
2 parents c07b81c + ac9b0a7 commit a7fa268
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/lib/helpers/project.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { page } from '$app/stores';
import { get } from 'svelte/store';

/**
* Returns the current project ID.
*
* The function first checks for a `project` parameter in the Svelte `$page` store.
* If not found, it extracts the project ID from the pathname.
*
* Supports:
* - Legacy structure: `/project-{projectID}/`
* - Multi-region structure: `/project-{region}-{projectID}/`
*
* Example:
* - `/project-console/` → `console`
* - `/project-fra-console/` → `console`
* - `/project-nyc-console/` → `console`
*/
export function getProjectId() {
// safety check!
const projectFromParams = get(page)?.params?.project;
if (projectFromParams) {
return projectFromParams;
}

const pathname = window.location.pathname + '/';
const projectMatch = pathname.match(/\/project-(.*?)\//);
const projectMatch = pathname.match(/\/project-(?:[a-z]{2,3}-)?([^/]+)/);

return projectMatch?.[1] || null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
return realtime
.forProject($page.params.region, $page.params.project)
.subscribe(['project', 'console'], (response) => {
console.log(response);
if (response.events.includes('stats.connections')) {
for (const [projectId, value] of Object.entries(response.payload)) {
stats.add(projectId, [new Date(response.timestamp).toISOString(), value]);
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/helpers/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test('getProjectId', () => {
pathname: string;
expected: string | null;
}> = [
// legacy
{
pathname: '/console/project-6374e9031588b25d1841/databases',
expected: '6374e9031588b25d1841'
Expand Down Expand Up @@ -34,6 +35,28 @@ test('getProjectId', () => {
{
pathname: '/console',
expected: null
},

// multi-region
{
pathname: '/console/project-fra-6374e9031588b25d1841/databases',
expected: '6374e9031588b25d1841'
},
{
pathname: '/console/project-/databases',
expected: null
},
{
pathname: '/console/project-nyc-project-project-abc/databases',
expected: 'project-project-abc'
},
{
pathname: '/console/project-fra-abc/databases',
expected: 'abc'
},
{
pathname: '/console/project-syd-abc/databases/project-123',
expected: 'abc'
}
];

Expand Down

0 comments on commit a7fa268

Please sign in to comment.