Skip to content

Commit 31362cd

Browse files
committed
improvement(config): consolidate socket server URL into getSocketServerUrl/getSocketUrl
Replace all inline `env.SOCKET_SERVER_URL || 'http://localhost:3002'` and `getEnv('NEXT_PUBLIC_SOCKET_URL') || 'http://localhost:3002'` with centralized utility functions in urls.ts, matching the getBaseUrl() pattern.
1 parent 6004f8b commit 31362cd

7 files changed

Lines changed: 35 additions & 17 deletions

File tree

apps/sim/app/api/workflows/[id]/state/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { z } from 'zod'
77
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
88
import { env } from '@/lib/core/config/env'
99
import { generateRequestId } from '@/lib/core/utils/request'
10+
import { getSocketServerUrl } from '@/lib/core/utils/urls'
1011
import { extractAndPersistCustomTools } from '@/lib/workflows/persistence/custom-tools-persistence'
1112
import {
1213
loadWorkflowFromNormalizedTables,
@@ -305,8 +306,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
305306
logger.info(`[${requestId}] Successfully saved workflow ${workflowId} state in ${elapsed}ms`)
306307

307308
try {
308-
const socketUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
309-
const notifyResponse = await fetch(`${socketUrl}/api/workflow-updated`, {
309+
const notifyResponse = await fetch(`${getSocketServerUrl()}/api/workflow-updated`, {
310310
method: 'POST',
311311
headers: {
312312
'Content-Type': 'application/json',

apps/sim/app/workspace/providers/socket-provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { createLogger } from '@sim/logger'
1414
import { useParams } from 'next/navigation'
1515
import type { Socket } from 'socket.io-client'
16-
import { getEnv } from '@/lib/core/config/env'
16+
import { getSocketUrl } from '@/lib/core/utils/urls'
1717
import { generateId } from '@/lib/core/utils/uuid'
1818
import {
1919
type SocketJoinCommand,
@@ -340,7 +340,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
340340
const initializeSocket = async () => {
341341
try {
342342
const { io } = await import('socket.io-client')
343-
const socketUrl = getEnv('NEXT_PUBLIC_SOCKET_URL') || 'http://localhost:3002'
343+
const socketUrl = getSocketUrl()
344344

345345
logger.info('Attempting to connect to Socket.IO server', {
346346
url: socketUrl,

apps/sim/lib/copilot/tools/handlers/workflow/mutations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
66
import type { ExecutionContext, ToolCallResult } from '@/lib/copilot/request/types'
77
import { env } from '@/lib/core/config/env'
88
import { generateRequestId } from '@/lib/core/utils/request'
9+
import { getSocketServerUrl } from '@/lib/core/utils/urls'
910
import { generateId } from '@/lib/core/utils/uuid'
1011
import { executeWorkflow } from '@/lib/workflows/executor/execute-workflow'
1112
import {
@@ -147,8 +148,7 @@ function findDescendants(containerId: string, blocksById: Record<string, BlockSt
147148
}
148149

149150
function notifyWorkflowUpdated(workflowId: string): void {
150-
const socketUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
151-
fetch(`${socketUrl}/api/workflow-updated`, {
151+
fetch(`${getSocketServerUrl()}/api/workflow-updated`, {
152152
method: 'POST',
153153
headers: {
154154
'Content-Type': 'application/json',

apps/sim/lib/copilot/tools/server/workflow/edit-workflow/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type ServerToolContext,
1010
} from '@/lib/copilot/tools/server/base-tool'
1111
import { env } from '@/lib/core/config/env'
12+
import { getSocketServerUrl } from '@/lib/core/utils/urls'
1213
import {
1314
applyTargetedLayout,
1415
getTargetedLayoutImpact,
@@ -284,8 +285,7 @@ export const editWorkflowServerTool: BaseServerTool<EditWorkflowParams, unknown>
284285

285286
logger.info('Workflow state persisted to database', { workflowId })
286287

287-
const socketUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
288-
fetch(`${socketUrl}/api/workflow-updated`, {
288+
fetch(`${getSocketServerUrl()}/api/workflow-updated`, {
289289
method: 'POST',
290290
headers: {
291291
'Content-Type': 'application/json',

apps/sim/lib/core/utils/urls.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getEnv } from '@/lib/core/config/env'
1+
import { env, getEnv } from '@/lib/core/config/env'
22
import { isProd } from '@/lib/core/config/feature-flags'
33

44
/** Canonical base URL for the public-facing marketing site. No trailing slash. */
@@ -100,3 +100,21 @@ export function getEmailDomain(): string {
100100
return isProd ? 'sim.ai' : 'localhost:3000'
101101
}
102102
}
103+
104+
const DEFAULT_SOCKET_URL = 'http://localhost:3002'
105+
106+
/**
107+
* Returns the socket server URL for server-side internal API calls.
108+
* Reads from SOCKET_SERVER_URL with a localhost fallback for development.
109+
*/
110+
export function getSocketServerUrl(): string {
111+
return env.SOCKET_SERVER_URL || DEFAULT_SOCKET_URL
112+
}
113+
114+
/**
115+
* Returns the socket server URL for client-side Socket.IO connections.
116+
* Reads from NEXT_PUBLIC_SOCKET_URL with a localhost fallback for development.
117+
*/
118+
export function getSocketUrl(): string {
119+
return getEnv('NEXT_PUBLIC_SOCKET_URL') || DEFAULT_SOCKET_URL
120+
}

apps/sim/lib/workflows/lifecycle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { env } from '@/lib/core/config/env'
1818
import { getRedisClient } from '@/lib/core/config/redis'
1919
import { PlatformEvents } from '@/lib/core/telemetry'
2020
import { generateRequestId } from '@/lib/core/utils/request'
21+
import { getSocketServerUrl } from '@/lib/core/utils/urls'
2122
import { mcpPubSub } from '@/lib/mcp/pubsub'
2223
import { getWorkflowById } from '@/lib/workflows/utils'
2324

@@ -31,8 +32,7 @@ interface ArchiveWorkflowOptions {
3132

3233
async function notifyWorkflowArchived(workflowId: string, requestId: string): Promise<void> {
3334
try {
34-
const socketUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
35-
const socketResponse = await fetch(`${socketUrl}/api/workflow-deleted`, {
35+
const socketResponse = await fetch(`${getSocketServerUrl()}/api/workflow-deleted`, {
3636
method: 'POST',
3737
headers: {
3838
'Content-Type': 'application/json',

apps/sim/lib/workflows/orchestration/deploy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { NextRequest } from 'next/server'
55
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
66
import { env } from '@/lib/core/config/env'
77
import { generateRequestId } from '@/lib/core/utils/request'
8-
import { getBaseUrl } from '@/lib/core/utils/urls'
8+
import { getBaseUrl, getSocketServerUrl } from '@/lib/core/utils/urls'
99
import { removeMcpToolsForWorkflow, syncMcpToolsForWorkflow } from '@/lib/mcp/workflow-mcp-sync'
1010
import { captureServerEvent } from '@/lib/posthog/server'
1111
import {
@@ -37,8 +37,7 @@ const logger = createLogger('DeployOrchestration')
3737
*/
3838
export async function notifySocketDeploymentChanged(workflowId: string): Promise<void> {
3939
try {
40-
const socketServerUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
41-
const response = await fetch(`${socketServerUrl}/api/workflow-deployed`, {
40+
const response = await fetch(`${getSocketServerUrl()}/api/workflow-deployed`, {
4241
method: 'POST',
4342
headers: {
4443
'Content-Type': 'application/json',
@@ -47,7 +46,9 @@ export async function notifySocketDeploymentChanged(workflowId: string): Promise
4746
body: JSON.stringify({ workflowId }),
4847
})
4948
if (!response.ok) {
50-
logger.warn(`Socket deployment notification failed (${response.status}) for workflow ${workflowId}`)
49+
logger.warn(
50+
`Socket deployment notification failed (${response.status}) for workflow ${workflowId}`
51+
)
5152
}
5253
} catch (error) {
5354
logger.error('Error sending workflow deployed event to socket server', error)
@@ -625,8 +626,7 @@ export async function performRevertToVersion(
625626
.where(eq(workflowTable.id, workflowId))
626627

627628
try {
628-
const socketServerUrl = env.SOCKET_SERVER_URL || 'http://localhost:3002'
629-
await fetch(`${socketServerUrl}/api/workflow-reverted`, {
629+
await fetch(`${getSocketServerUrl()}/api/workflow-reverted`, {
630630
method: 'POST',
631631
headers: {
632632
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)