W
Wasp-lang3mo ago
Sam79

Can't connect to WASP WebSocket's on Intranet

I'm using WASP 0.14.0 on Mac OS 14.6 and am trying to run the WebSocket example: https://wasp-lang.dev/docs/advanced/web-sockets so that its available on my intranet. However, I can only connect to it on the machine itself with localhost (which works ok). I tried a Socket IO example socket.io-chat-platform and was able to get that working on my network so I know it's not a Firewall issue. Also HTTP access to the WASP app on the network works ok. How can I run the WASP app so WebSockets work on the network?
Web Sockets | Wasp
Wasp provides a fully integrated WebSocket experience by utilizing Socket.IO on the client and server.
7 Replies
kapa.ai
kapa.ai3mo ago
Hi @Sam79. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. However, I can guide you through some general steps to ensure your WebSocket connection is set up correctly in Wasp. 1. Enable WebSockets in your Wasp file: You need to specify that you are using WebSockets by adding webSocket to your app and providing the required fn. You can optionally change the auto-connect behavior. Here is an example:
app todoApp {
// ...
webSocket: {
fn: import { webSocketFn } from "@src/webSocket",
autoConnect: true, // optional, default: true
},
}
app todoApp {
// ...
webSocket: {
fn: import { webSocketFn } from "@src/webSocket",
autoConnect: true, // optional, default: true
},
}
Source (https://wasp-lang.dev/docs/advanced/web-sockets) 2. Define your WebSocket logic on the server: You need to define the WebSocket events and handlers on the server side. Here is an example of how you can define your webSocketFn function:
export const webSocketFn = (io, context) => {
io.on('connection', (socket) => {
const username = getFirstProviderUserId(socket.data.user) ?? 'Unknown'
console.log('a user connected: ', username)

socket.on('chatMessage', async (msg) => {
console.log('message: ', msg)
io.emit('chatMessage', { id: uuidv4(), username, text: msg })
// You can also use your entities here:
// await context.entities.SomeEntity.create({ someField: msg })
})
})
}
export const webSocketFn = (io, context) => {
io.on('connection', (socket) => {
const username = getFirstProviderUserId(socket.data.user) ?? 'Unknown'
console.log('a user connected: ', username)

socket.on('chatMessage', async (msg) => {
console.log('message: ', msg)
io.emit('chatMessage', { id: uuidv4(), username, text: msg })
// You can also use your entities here:
// await context.entities.SomeEntity.create({ someField: msg })
})
})
}
Source (https://wasp-lang.dev/docs/advanced/web-sockets#defining-the-events-handler) 3. Use WebSockets on the client: You can use WebSockets on the client, in React, via useSocket and useSocketListener. Here is an example:
const { socket, isConnected } = useSocket()
useSocketListener('chatMessage', logMessage)
const { socket, isConnected } = useSocket()
useSocketListener('chatMessage', logMessage)
Source (https://wasp-lang.dev/docs/advanced/web-sockets#usesocketlistener-hook) If you've followed these steps and are still having issues, I recommend checking the Wasp GitHub issues (https://github.com/wasp-lang/wasp/issues) to see if anyone else has encountered a similar problem. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sam79, 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?
Sam79
Sam793mo ago
@kapa.ai I see it's likely a configuration issue. When I do wasp start I can see the http server will listen to the network interface: 192.168.x.x on port 3000 However the websocket server only listens to localhost on port 3001 so its not reachable. It's not clear where I can configure it. It doesn't seem to be part of the Vite configuration. @kapa.ai any idea how to configure this?
MEE6
MEE63mo ago
Wohooo @Sam79, you just became a Waspeteer level 1!
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry, but the knowledge sources provided do not contain specific information on how to configure Wasp for WebSocket connections on an intranet network. However, here are some general steps you can follow: 1. Check your network settings: Ensure that your network allows WebSocket connections and that the necessary ports are open. 2. Check your server configuration: Make sure your Wasp server is set up to accept connections from other devices on your network. This usually involves binding the server to 0.0.0.0 (which represents all available network interfaces) instead of localhost (which represents the local machine only). However, the provided knowledge sources do not specify how to do this in Wasp. 3. Check your client configuration: Ensure that your client is trying to connect to the correct network address of your server, not localhost. If you're still having trouble, I recommend reaching out to the Wasp community or checking the Wasp documentation (https://wasp-lang.dev/docs) for more information. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sam79, 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?
Sam79
Sam793mo ago
@kapa.ai How do I configure step 2: binding the websocket server to 0.0.0.0 in WASP?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry, but the provided knowledge sources do not contain specific information on how to bind the WebSocket server to 0.0.0.0 in Wasp. Wasp's WebSocket functionality is built on top of Socket.IO, and typically in a Node.js/Socket.IO application, you would specify the host when setting up your server, like so:
const server = require('http').createServer();
const io = require('socket.io')(server);
server.listen(port, '0.0.0.0');
const server = require('http').createServer();
const io = require('socket.io')(server);
server.listen(port, '0.0.0.0');
However, Wasp abstracts away much of this setup, and it's not clear from the provided sources if or how you can specify the host for the WebSocket server. I recommend reaching out to the Wasp community on their Discord server (https://discord.gg/rzdnErX) or checking the Wasp GitHub issues (https://github.com/wasp-lang/wasp/issues) for more specific guidance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sam79, 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?
Filip
Filip2mo ago
Hey @Sam79, websockets should work on the internet out of the box. From what I got, everything worked OK locally ( your client successfully connected to the server using websockets)? Where is it broken? Is it after you deploy your app. If that's the case, where have you deployed it?
Want results from more Discord servers?
Add your server