nestjs: ws:// works but wss://localhost:port/ got ‘WebSocket connection to 'wss://localhost:10086/' failed: ‘ - ssl

i was trying to create wss server in using nestjs,but when i finished all configuration in a new nestjs project and tried to connect to the server via 'wss://localhost:port', i got the error below:
WebSocket connection to 'wss://localhost:10033/' failed:
enter image description here
and the main.ts is like this:
async function bootstrap() {
const {key, cert} = await readKeyAndCert()
const app = await NestFactory.create(AppModule, {
httpsOptions: {
requestCert: false,
rejectUnauthorized: false,
key,cert
}
});
app.enableCors()
app.useWebSocketAdapter(new WsAdapter(app))
app.useGlobalFilters(
new HttpExceptionFilter()
)
await app.listen(HTTP_PORT);
}
what can i do to fix the wss:// connection?

Related

Express JS Websocket Connection to wss failed without error but still able to communicate

I am using an Express JS to initiate websocket connection. Below is the following configuration that I used.
socket.js
const ip_address = `${process.env.MIX_HTTPS_APP_URL}`;
const socket_port = `${process.env.MIX_EXPRESS_PORT}`;
const URL = ip_address + ":" + socket_port;
export const socket = io(URL, { autoConnect: true });
socket.onAny((event, ...args) => {
// console.log(event, args);
});
index.js
const app = express();
// app settings
/** Create HTTP server. */
const privateKey = fs.readFileSync(env.privateKey, "utf8");
const certificate = fs.readFileSync(env.certificate, "utf8");
const options = {
key: privateKey,
cert: certificate,
};
const server = https.createServer(options, app).listen(port);
/** Create socket connection */
const socketio = new Server(server);
global.io = socketio.listen(server, {
cors: {
origin: env.url,
},
});
global.io.on("connection", WebSockets.connection);
/** Listen on provided port, on all network interfaces. */
/** Event listener for HTTP server "listening" event. */
server.on("listening", () => {
console.log(`Listening on port:: ${env.url}:${port}/`);
});
Though I can emit and listen to socket, I am still getting this error message. How should I solve this so that users wont see this error message when they view the console?
With reference to your example, the listening on port should be on http server, & socket.io should just attach to on new conn.
see doc https://socket.io/docs/v4/server-initialization/#with-an-https-server
Hope this helps.

Using Secure Websocket on 3 different ports in Nest.js

I have a Nest-Service with the following main.ts:
async function bootstrap() {
if (!!environment.production) {
const app = await NestFactory.create(AppModule, {
httpsOptions: {
key: fs.readFileSync(environment.ssl.SSL_KEY_PATH),
cert: fs.readFileSync(environment.ssl.SSL_CERT_PATH)
},
});
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(3077);
} else {
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(3077);
}
}
bootstrap();
And two Gateways within the Service:
#WebSocketGateway(3078)
export class ItemsGateway implements OnGatewayConnection, OnGatewayDisconnect { ... }
#WebSocketGateway(3079)
export class UnitsGateway implements OnGatewayConnection, OnGatewayDisconnect { ... }
Without SSL this is working, but when I use the prod mode I can´t establish a secure connection to domain.tld:3078 and :3079.
How can I get the service to listen on all 3 Ports? I think there is the problem, because certs are only attached to the Server listening on Port: 3077, where all my REST-API stuff goes.
Thx, Dom
Edit: This also worked as there was just on WebsocketServer on the same port as the API -> 3077.
Edit 2:
I also tried this, but then comes the error that address is in use on the second attempt to create() a server:
async function bootstrap() {
if (!!environment.production) {
const httpsOptions = {
key: fs.readFileSync(environment.ssl.SSL_KEY_PATH),
cert: fs.readFileSync(environment.ssl.SSL_CERT_PATH)
};
const server = express();
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(server)
);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.init();
https.createServer(httpsOptions, server).listen(environment.app.port);
https.createServer(httpsOptions, server).listen(environment.websocketPorts.units);
https.createServer(httpsOptions, server).listen(environment.websocketPorts.items);
} else {
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));
app.enableCors();
await app.listen(environment.app.port);
}
}
bootstrap();
You need to .create() a separate app for each port on which you listen for wss connections.

How to control when socket connection request is sent to backend from client?

Using vue-socket.io my frontend is currently firing off a Socket.io connection request to my backend when my app is being built initially or every time I refresh the page. This is problematic for me as my page gets initially built at the landing page. This means that it is sending a socket request without the proper data since the user hasn't logged in yet. Once the user logs in I am unable to send another socket connection request.
socket.js (plugins folder) FRONTEND
import Vue from "vue";
import store from "../store";
import SocketIO from "socket.io-client";
import VueSocketIO from "vue-socket.io";
Vue.use(
new VueSocketIO({
debug: true,
connection: SocketIO("http://localhost:3000", {
query: { token: store.state.token }
}),
vuex: {
store,
actionPrefix: "SOCKET_",
mutationPrefix: "SOCKET_"
}
})
);
socket.js (controllers folder) BACKEND
io.use(function (socket, next) {
console.log('Token ' + socket.handshake.query.token);
if (socket.handshake.query && socket.handshake.query.token) {
jwt.verify(socket.handshake.query.token, 'THIS IS SUPPOSED TO BE PRIVATE', function (err, decoded) {
if (err) return next(new Error('Authentication error'));
socket.decoded = decoded;
next();
});
} else {
next(new Error('Authentication error'));
}
})
.on('connection', function (socket) {
socket.on('JOIN_ROOM', (room) => {
socket.join(room);
});
// CourseQuestions Page
socket.on('POST_QUESTION', (data) => {
I am looking for a way to programmatically send off the socket connection request AGAIN from the frontend once the user has logged in preferable using vue-socket.io.
You should pass { autoConnect: false } as options into new VueSocketIO().
Something like this:
new VueSocketIO(..., { autoConnect: false }, ...)
And then, when you want to open the connection just call this.$socket.open().
GitHub issue. and check out that comment.

Peerjs keeps loosing connection and user id is lost

I have made an application following a tutorial using peerjs. Everything seems to be working fine except when I make a connection for a video call where I am using peerjs. I have made my own peerjs server which I am running on localhost (right now for testing). Here is the code for the peer server:
const express = require('express');
const path = require('path');
const http = require('http');
const cors = require('cors');
const errorhandler = require('errorhandler');
var ExpressPeerServer = require('peer').ExpressPeerServer;
var options = {
debug: true,
key: 'copycat'
};
var app = express();
var server = http.createServer(app);
var port = process.env.PORT || '3001';
app.set('port', port);
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/peerjs', ExpressPeerServer(server, options));
app.use(errorhandler());
process.on('uncaughtException', function(exc) {
console.error(exc);
});
server.listen(port);
As you can see I am running the app on port 3001. Now following is the script for peerjs connection for a video call:
// PeerJS
// Compatibility shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// PeerJS object
var peer = new Peer(username + roomId, {
host: 'localhost',
path: '/peerjs',
port: 443,
secure: true,
key: 'copycat',
debug: true
});
peer.on('open', function () {
$('#my-id').text(peer.id);
});
// Receiving a call
peer.on('call', function (call) {
// Answer the call automatically (instead of prompting user) for demo purposes
call.answer(window.localStream);
step3(call);
});
peer.on('error', function (err) {
alert(err.message);
// Return to step 2 if error occurs
step2();
});
// Click handlers setup
$(function () {
$('#make-call').click(function () {
// Initiate a call!
var call = peer.call($('#callto-id').val(), window.localStream);
step3(call);
});
$('#end-call').click(function () {
window.existingCall.close();
step2();
});
step1();
});
function step1() {
// Get audio/video stream
navigator.getUserMedia({ audio: true, video: true }, function (stream) {
// Set your video displays
$('#my-video').prop('src', URL.createObjectURL(stream));
window.localStream = stream;
step2();
}, function () { $('#step1-error').show(); });
}
function step2() {
$('#step1, #step3').hide();
$('#step2').show();
}
function step3(call) {
// Hang up on an existing call if present
if (window.existingCall) {
window.existingCall.close();
}
// Wait for stream on the call, then set peer video display
call.on('stream', function (stream) {
$('#second-video').prop('src', URL.createObjectURL(stream));
});
// UI stuff
window.existingCall = call;
$('#second-id').text(call.peer);
call.on('close', step2);
$('#step1, #step2').hide();
$('#step3').show();
}
This is pretty much the example code from peerjs example file on github. What I am confused about is the port value. Inside the options in the above script its port 443. I get the following error in chrome when I try to make a video call:
peer.js:1492 WebSocket connection to 'wss://localhost/peerjs/peerjs?key=peerjs&id=User80925be509c6c606fa21409858f5&token=zz69b3ccyk' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Socket._startWebSocket # peer.js:1492
Socket.start # peer.js:1481
Peer._initialize # peer.js:1058
Peer # peer.js:962
(anonymous) # 5be509c6c606fa21409858f5:183
peer.js:1741 PeerJS: Socket closed.
peer.js:1741 PeerJS: ERROR Error: Lost connection to server.
peer.js:1555 POST https://localhost/peerjs/peerjs/User80925be509c6c606fa21409858f5/zz69b3ccyk/id?i=0 net::ERR_CONNECTION_REFUSED
Socket._startXhrStream # peer.js:1555
Socket.start # peer.js:1480
Peer._initialize # peer.js:1058
Peer # peer.js:962
(anonymous) # 5be509c6c606fa21409858f5:183
peer.js:1741 PeerJS: ERROR Error: Lost connection to server.
Please advise what am I doing wrong???
If you are using at your local end then use your localport i.e. 3001 else use 443
make object like this
var peer = new Peer(undefined, {
host: 'localhost',
path: '/peerjs',
port: 3001,
secure: true,
key: 'copycat',
debug: true
});

How to access data in Parse Server via Cloud Code?

I try to make queries using the cloud code feature of our Parse server. Unfortunately we could not retrieve any data from the database. Our code looks as follows:
main.js:
Parse.Cloud.define('test', function(request, response) {
var user = request.user;
var token = user.getSessionToken();
var query = new Parse.Query('Carpark');
query.first({ sessionToken: token }) // pass the session token to find()
.then(function(messages) {
response.success(messages);
}, function(error) {
response.error(error);
});
});
index.js:
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var api = new ParseServer({
databaseURI: 'mongodb://parse-server:[...]#localhost:27017/[...]',
cloud: __dirname + '/cloud/main.js',
appId: '[...]',
masterKey: '[...], //Add your master key here. Keep it secret!
serverURL: 'https://backend.[...]/parse', // Don't forget to change to https if needed
publicServerURL: 'https://backend.[...]/parse', // Don't forget to change to https if needed
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
var app = express();
//var basicAuth = require('basic-auth-connect');
//app.use(basicAuth('triveme', 'triveme'));
app.use('/', express.static(path.join(__dirname, '/public')));
app.use('/parse', api);
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});
var port = 61004;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);
Example request from iOS-App:
PFCloud.callFunction(inBackground: "test", withParameters: nil) {
(response, error) -> Void in
if let response = response {
let result = response
print("Cloud data:", result )
}
if error != nil {
print(error ?? "default cloud function error")
}
}
I don't get any feedback from server (no response and no error). What is a possible problem of my issue?
Log request with verbose = 1:
REQUEST for [POST] /parse/functions/test: {} method=POST, url=/parse/functions/test, host=localhost:61004, accept=*/*, x-parse-session-token=[...], x-parse-application-id=[...].platform.dev2, x-parse-installation-id=[...], x-parse-os-version=10.2 (16D32), accept-language=en-us, accept-encoding=gzip, deflate, x-parse-client-version=i1.14.2, user-agent=trive.park/8 CFNetwork/808.2.16 Darwin/16.4.0, x-parse-app-build-version=8, x-parse-app-display-version=1.0, x-forwarded-for=[...], x-forwarded-host=backend.[...], x-forwarded-server=backend.[...], connection=Keep-Alive, content-length=0,
Log Response:
4|trive-pa | error: Failed running cloud function test for user nZ76ZimELw with:
4|trive-pa | Input: {}
4|trive-pa | Error: {"code":141,"message":{"code":100,"message":"XMLHttpRequest failed: \"Unable to connect to the Parse API\""}} functionName=test, code=141, code=100, message=XMLHttpRequest failed: "Unable to connect to the Parse API", user=nZ76ZimELw
4|trive-pa | error: Error generating response. ParseError {
4|trive-pa | code: 141,
4|trive-pa | message:
4|trive-pa | ParseError {
4|trive-pa | code: 100,
4|trive-pa | message: 'XMLHttpRequest failed: "Unable to connect to the Parse API"' } } code=141, code=100, message=XMLHttpRequest failed: "Unable to connect to the Parse API"
4|trive-pa | [object Object]
parse-server version: 2.2.23
self hosted on Apache Server
MongoDB