diff --git a/waspc/data/Generator/templates/react-app/src/index.tsx b/waspc/data/Generator/templates/react-app/src/index.tsx index 6ecad2d2a4..cf57444d12 100644 --- a/waspc/data/Generator/templates/react-app/src/index.tsx +++ b/waspc/data/Generator/templates/react-app/src/index.tsx @@ -14,7 +14,7 @@ import { {=/ setupFn.isDefined =} {=# areWebSocketsUsed =} -import { WebSocketProvider } from 'wasp/webSocket/WebSocketProvider' +import { WebSocketProvider } from 'wasp/client/webSocket/WebSocketProvider' {=/ areWebSocketsUsed =} startApp() diff --git a/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx b/waspc/data/Generator/templates/sdk/client/webSocket/WebSocketProvider.tsx similarity index 95% rename from waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx rename to waspc/data/Generator/templates/sdk/client/webSocket/WebSocketProvider.tsx index 062aba19d8..d4e37e648c 100644 --- a/waspc/data/Generator/templates/sdk/webSocket/WebSocketProvider.tsx +++ b/waspc/data/Generator/templates/sdk/client/webSocket/WebSocketProvider.tsx @@ -8,11 +8,12 @@ import config from 'wasp/core/config' import type { ClientToServerEvents, ServerToClientEvents } from 'wasp/server/webSocket'; +// PRIVATE API // TODO: In the future, it would be nice if users could pass more // options to `io`, likely via some `configFn`. export const socket: Socket = io(config.apiUrl, { autoConnect: {= autoConnect =} }) -function refreshAuthToken() { +function refreshAuthToken() { // NOTE: When we figure out how `auth: true` works for Operations, we should // mirror that behavior here for WebSockets. Ref: https://github.com/wasp-lang/wasp/issues/1133 socket.auth = { @@ -29,11 +30,13 @@ refreshAuthToken() apiEventsEmitter.on('sessionId.set', refreshAuthToken) apiEventsEmitter.on('sessionId.clear', refreshAuthToken) +// PRIVATE API export const WebSocketContext = createContext({ socket, isConnected: false, }); +// PRIVATE API export function WebSocketProvider({ children }: { children: JSX.Element }) { const [isConnected, setIsConnected] = useState(socket.connected) diff --git a/waspc/data/Generator/templates/sdk/webSocket/index.ts b/waspc/data/Generator/templates/sdk/client/webSocket/index.ts similarity index 95% rename from waspc/data/Generator/templates/sdk/webSocket/index.ts rename to waspc/data/Generator/templates/sdk/client/webSocket/index.ts index 3dce7da720..ccd6ce9e46 100644 --- a/waspc/data/Generator/templates/sdk/webSocket/index.ts +++ b/waspc/data/Generator/templates/sdk/client/webSocket/index.ts @@ -5,15 +5,19 @@ import type { ServerToClientEvents, } from 'wasp/server/webSocket' +// PUBLIC API export type ServerToClientPayload = Parameters[0] +// PUBLIC API export type ClientToServerPayload = Parameters[0] +// PUBLIC API export function useSocket() { return useContext(WebSocketContext) } +// PUBLIC API export function useSocketListener( event: Event, handler: ServerToClientEvents[Event] diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index e853eed44a..fb0a46e365 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -89,14 +89,12 @@ "./server/jobs/pgBoss/types": "./dist/server/jobs/pgBoss/types.js", {=! Used by users, documented. =} "./server/webSocket": "./dist/server/webSocket/index.js", - {=! Used by users, documented. =} - "./webSocket": "./dist/webSocket/index.js", - {=! Used by our code, uncodumented (but accessible) for users. =} - "./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx", + {=! Used by the framework client code, reconsider during refactoring. =} + "./client/webSocket/WebSocketProvider": "./dist/client/webSocket/WebSocketProvider.jsx", {=! Still needed, reconsider during refactoring. =} "./server/types": "./dist/server/types/index.js", - {=! Still used by the server code, reconsider during refactoring. =} + {=! Used by the framework server code, reconsider during refactoring. =} "./server/middleware": "./dist/server/middleware/index.js", {=! ================= NEW API HERE =================== =} @@ -109,6 +107,9 @@ {=! Public: { api } =} {=! Private: [sdk] =} "./client/api": "./dist/api/index.js", + {=! Public: { type ServerToClientPayload, type ClientToServerPayload, useSocket, useSocketListener } =} + {=! Private: [] =} + "./client/webSocket": "./dist/client/webSocket/index.js", "./auth": "./dist/auth/index.js", "./client/auth": "./dist/client/auth/index.js", "./server/auth": "./dist/server/auth/index.js", diff --git a/waspc/examples/todo-typescript/src/ChatPage.tsx b/waspc/examples/todo-typescript/src/ChatPage.tsx index 30f8c3191f..6978a10c09 100644 --- a/waspc/examples/todo-typescript/src/ChatPage.tsx +++ b/waspc/examples/todo-typescript/src/ChatPage.tsx @@ -4,7 +4,7 @@ import { useSocket, useSocketListener, ServerToClientPayload, -} from 'wasp/webSocket' +} from 'wasp/client/webSocket' async function fetchCustomRoute() { const res = await api.get('/foo/bar') diff --git a/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs index 187778be18..ae6758780e 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs @@ -26,7 +26,7 @@ genWebSockets spec | AS.WS.areWebSocketsUsed spec = sequence [ genWebSocketServerIndex spec, - genFileCopy [relfile|webSocket/index.ts|], + genFileCopy [relfile|client/webSocket/index.ts|], genWebSocketProvider spec ] | otherwise = return [] @@ -46,7 +46,7 @@ genWebSocketServerIndex spec = return $ C.mkTmplFdWithData [relfile|server/webSo mayebWebSocketFn = AS.App.WS.fn <$> maybeWebSocket genWebSocketProvider :: AppSpec -> Generator FileDraft -genWebSocketProvider spec = return $ C.mkTmplFdWithData [relfile|webSocket/WebSocketProvider.tsx|] tmplData +genWebSocketProvider spec = return $ C.mkTmplFdWithData [relfile|client/webSocket/WebSocketProvider.tsx|] tmplData where maybeWebSocket = AS.App.webSocket $ snd $ getApp spec shouldAutoConnect = (AS.App.WS.autoConnect <$> maybeWebSocket) /= Just (Just False)