Hey I have an issue contecting my expo app by websockets with my webserver.
First I tested my websockets of my app with WebSockets - π©βπ» Do tests for FREE πͺ - SocketsBay.com and they work.
After that I implemented an express server. And I can get a connection with postman through the local address ws://127.0.0.1:4000/
My problem: I cannot connect to the websocket server with my expo app.
I think the problem is, that I cannot take the localhost ip address, since the app is running on my phone. So I have to use the local ip adress within the local wifi-network of my laptop. But this is also not working. I donβt know why. Hope someone can help me.
My websocket server is very basic:
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import express from 'express'
import expressWs from 'express-ws'
import http from 'http'
let port = 4000;
let app = express();
let server = http.createServer(app);
expressWs(app, server);
app.use(express.static(__dirname + '/views'));
app.get('/', (req, res) => {
console.log("Request on '/'")
res.status(200).send("Welcome to our app");
});
app.ws('/', async function (ws, req) {
console.log("New websocket connection")
ws.send("Got connected to server.")
ws.on('message', async function (msg) {
console.log("Got new message: ")
console.log(msg); //this only gives me and output when I send with postman, not with my app
});
});
server.listen(port, () => {
console.log("Server runs on port %s...", port)
})