1

I have this code which I am using for my express server. The server works when I access the routes over http however when I try to connect to it over wss it doesnt work. What am I doing wrong?

const morgan = require("morgan");
const cors = require("cors");
const passport = require("passport");
const localStrategy = require("./auth/local");
const jwtStrategy = require("./auth/jwt");
const expressValidator = require("express-validator");
const config = require("./config");

const app = express();

const http = require("http").Server(app);
const io = require("socket.io")(http);

app.use(cors());
app.use(express.json());
app.use(expressValidator());
app.use(morgan("common"));
app.use(passport.initialize());

passport.use(localStrategy);
passport.use(jwtStrategy);

require("./routes")(app);

io.on("connect_error", (err) => {
  console.log(`connect_error due to ${err.message}`);
});
io.on("connection", (socket) => {
  console.log("a user connected", socket);
});

http.listen(config.port);

module.exports = app;
2
  • You can look at this example, it uses a slightly different pattern. But the problem might also be on your client side. Commented Jan 20, 2022 at 0:40
  • I am using this website to test it. piesocket.com/websocket-tester Commented Jan 20, 2022 at 0:47

1 Answer 1

2

Try using ws: in place of wss: for a quick fix.

wss: is the TLS-secured version of ws:, analogous to https: and http:. In fact, websocket connections to a server begin their lives as http / https connections. They're then "upgraded" to websocket connections by the websocket protocol.

So, to get wss: to work on that server of yours, start by getting https: working for the express webserver part. It's the https server behind express that will also handle the connections to be upgraded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.