Hi @HTML_Celeb, in case it helps, this is how I’m doing it on my project on both client and server side. It uses an HTTPS (and hence wss) connection, so it requires a little bit more configuration (credentials, cors):
/* Server side */
var cors = require('cors')
const express = require('express')()
express.use(cors())
var httpsServer = require('https').createServer(credentials, express)
// Server with CORS enabled and WS protocol for transport only
var wsServer = require('socket.io')(httpsServer, {
cors: {
origin: "*",
methods: ["GET", "POST"]
},
transports: ['websocket']
})
wsServer.sockets.on('connection', (socket) => {
console.log("Client has connected!")
socket.emit('customEvent', 'hello')
})
[...]
// Make sure your HTTP server is listening on the right port and address
httpsServer.listen(3000, '0.0.0.0')
And then in the client side (make sure you’ve got your socket.io javascript file included in your PlayCanvas project so it can load properly):
/* Client side (e.g your network.js script) */
var socket = io.connect('wss://<your_server_endpoint>:3000', { transports: ['websocket'] });
socket.on('customEvent', function(data){
console.log(data) // should print 'hello'
})
Hope it helps.