WebSockets outside wasp

How can I access a websocket connection from wasp in other applications?
6 Replies
kapa.ai
kapa.ai3w ago
Hi @NightOwl. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
MEE6
MEE63w ago
Wohooo @NightOwl, you just became a Waspeteer level 1!
kapa.ai
kapa.ai3w ago
---------------------- To access a WebSocket connection from Wasp in other applications, you currently have limited options as Wasp's WebSocket functionality is primarily designed for use within the Wasp ecosystem. However, there are a few approaches you can consider: 1. Use a workaround to expose the WebSocket instance: As mentioned in the GitHub issue about enabling WebSocket usage outside the init function (https://github.com/wasp-lang/wasp/issues/1289), you can create a global variable to store the WebSocket instance and export it for use in other parts of your Wasp backend. This approach allows you to access the WebSocket instance within your Wasp server-side code, but not directly in external applications. 2. Create an HTTP API: You can create an HTTP API in your Wasp application that external applications can interact with. This API can then internally use the WebSocket connection to communicate with clients. This approach is mentioned in a GitHub comment (https://github.com/wasp-lang/wasp/issues/1475#issuecomment-1944486708) as a potential solution for supporting multiple clients. 3. Use a proxy: As discussed in a GitHub issue about adding a proxy for the dev server (https://github.com/wasp-lang/wasp/issues/1255), you could set up a proxy in front of your Wasp application. This proxy could potentially handle WebSocket connections and route them appropriately, allowing external applications to connect. 4. Wait for future Wasp updates: The Wasp team is aware of the need for better support for multiple clients and external access to WebSockets. As mentioned in a GitHub comment (https://github.com/wasp-lang/wasp/issues/1475#issuecomment-1944486708), they are considering options like generating SDKs or APIs that could be used by external applications. It's important to note that currently, Wasp does not have built-in support for easily exposing WebSocket connections to external applications. The WebSocket functionality is primarily designed for use within the Wasp ecosystem, as described in the Wasp WebSockets documentation (https://wasp-lang.dev/docs/advanced/web-sockets). If you need robust WebSocket support across multiple applications, you might need to consider alternative approaches or wait for future Wasp updates that may provide better support for this use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NightOwl, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
NightOwl
NightOwlOP3w ago
@kapa.ai I want to use it in another client app which does not use wasp. Is that possible?
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the current information available about Wasp's WebSocket implementation, using it directly in another client app that doesn't use Wasp is not straightforward or officially supported. However, there are a few potential workarounds you could consider: 1. HTTP API Approach: You could create an HTTP API in your Wasp application that your non-Wasp client app can interact with. This API could then internally use WebSockets to communicate with Wasp clients. This approach is mentioned in a GitHub comment (https://github.com/wasp-lang/wasp/issues/1475#issuecomment-1944486708) as a potential solution for supporting multiple clients. 2. Custom SDK: As discussed in the GitHub issue about supporting multiple clients (https://github.com/wasp-lang/wasp/issues/1475), the Wasp team is considering the possibility of providing SDKs or APIs that could be used by external applications. However, this is not currently available and is more of a future consideration. 3. Direct WebSocket Connection: If your Wasp application exposes a WebSocket endpoint, theoretically, you could connect to it directly from your non-Wasp client using a standard WebSocket library. However, this approach might not be officially supported and could lack some of the built-in features that Wasp provides. It's important to note that Wasp's WebSocket functionality is primarily designed for use within the Wasp ecosystem, as described in the Wasp WebSockets documentation (https://wasp-lang.dev/docs/advanced/web-sockets). The current implementation doesn't have built-in support for easily exposing WebSocket connections to external applications. For now, if you need robust WebSocket support across multiple applications, including non-Wasp clients, you might need to consider alternative approaches or wait for future Wasp updates that may provide better support for this use case. The Wasp team seems to be aware of this need and is considering options for the future, but there's no concrete timeline for such features. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NightOwl, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho3w ago
Wasp's server is just a Node.js app that is running socket.io for the Websockets. It should just work but I guess you'll need to configure CORS to work: https://gist.github.com/infomiho/5ca98e5e2161df4ea78f76fc858d3ca2 And then use the socket.io client to connect to the Wasp backend. The bigger problem could be authentication - you'd need the session ID to auth your self. I'm not sure about the code - you can find it in the generated React app, but it should be a matter of sending a message with the session ID.
Gist
Multiple domains for CORS - custom global middleware in Wasp 0.12+
Multiple domains for CORS - custom global middleware in Wasp 0.12+ - main.wasp
Want results from more Discord servers?
Add your server