I am new to javaScript. I have written 2 js files to test out web sockets.
server.js
const io = require('socket.io')(8000)
const users = {}
io.on('connection', socket => {
console.log('new user')
})
io.on('disconnect', socket => {
console.log("user left")
})
client.js
var socket = io('https://localhost:8000', { transports : ['websocket'] });
However, the connection cannot be established. Error messages:
chrome
WebSocket connection to 'wss://localhost:8000/socket.io/?EIO=4&transport=websocket' failed:
firefox
Firefox can’t establish a connection to the server at wss://localhost:8000/socket.io/?EIO=4&transport=websocket. 2
I am working on ubuntu 20.04
Your server initialization is probably nit correct. You test the connectivity using ping 127.0.0.1:8000 command on shell after starting server. Try initializing server using the code below:
const { Server } = require("socket.io");
const io = new Server({ /* options */ });
io.on("connection", (socket) => {
// ...
});
io.listen(8000);
Detaiked server initialisation info can be found at docs: https://socket.io/docs/v4/server-initialization/