@edmond_brakus
To use socket.io with HTTPS, you need to make some changes to your server code. Here is a step-by-step guide on how to do it:
1
|
openssl req -nodes -new -x509 -keyout server.key -out server.cert |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const https = require('https'); const fs = require('fs'); const socketIO = require('socket.io'); const options = { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.cert') }; const server = https.createServer(options); const io = socketIO(server); io.on('connection', (socket) => { console.log('A user connected'); }); server.listen(3000, () => { console.log('Server running on port 3000'); }); |
1
|
const socket = io('https://yourdomain.com:3000'); |
That's it! You have now set up socket.io with HTTPS. Make sure to replace 'yourdomain.com:3000'
with your actual server domain and port number.