I'm new to WS and Heroku and all that... so i have this code
//this sets up client-side sockets i guess
import {io} from 'socket.io-client';
const options = {
"force new connection": true,
reconnectionAttempts: "Infinity",
timeout : 10000,
transports : ["websocket"]
}
const socket = io('/', options)
export default socket;
and for the server side
const path = require('path');
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const {version, validate} = require('uuid');
const ACTIONS = require('./src/socket/actions');
const PORT = process.env.PORT || 3001;
///more code
When deployed to heroku this results in this app https://lit-atoll-99067.herokuapp.com/ and the chrome console says :
WebSocket connection to 'wss://lit-atoll-99067.herokuapp.com/socket.io/?EIO=4&transport=websocket' failed: WebSocket is closed before the connection is established.
So i ran out of ideas. but i guess this has to be about port or something... dunno really. Any ideas are welcome!
I think you need to pass the full URL at the client-side but you only passed the "/" only example:-
import {io} from 'socket.io-client';
const options = {
"force new connection": true,
reconnectionAttempts: "Infinity",
timeout : 10000,
transports : ["websocket"]
}
// here need to pass the full url
const socket = io('https://example.com/', options)
export default socket;
And for the server side try this
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});