obs-websocket-js failed to connect. Server sent no subprotocol - node.js

I want to use obs-websocket-js package to build an app to connect to OBS Studio through websocket.
When I try running the connection function, I get a Failed to connect -1 Server sent no subprotocol error.
The OBS Studio is running on Ubuntu 20.01 which is on a Oracle VM VirtualBox on my system.
Node.JS code:
const OBSWebSocket = require("obs-websocket-js").default;
const obs = new OBSWebSocket();
const connect = async () => {
try {
const { obsWebSocketVersion, negotiatedRpcVersion } = await obs.connect("ws://192.168.100.170:4444",undefined, {rpcVersion: 1});
return `Connected to server ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`;
} catch (error) {
console.error("Failed to connect", error.code, error.message);
}
};
connect().then((value) => console.log(value));
I expect to make a succesful connection and to find out why I have a Server sent no subprotocol error.

I found a very simple solution: I updated the OBS Studio to the latest 29.0.0 version and it worked.

Related

Add data to Redis on a distant server

Beginner to redis here.
I'm trying to CRUD data from a distant redis server but I don't seem to make it work :
First we connect to the server :
const client = redis.createClient("the PORT", "the host", {auth_pass: "the password"});
(async () => {
try {
await client.connect();
console.log('Connected to redis');
} catch (err) {
console.error(err)
}
})()
The connection seems to work, as I receive a
Connected to redis in my terminal
But when I try to add data, it doesn't seem to work
app.get('/redis', (req, res) => {
// set name to antoine on redis
client.set('name', 'antoine', redis.print);
// get name from redis
client.get('name', (err, name) => {
res.send(name);
}
);
});
The endpoint just load indefinitely as if it doesn't do the
client.get('name', (err, name) => {
res.send(name);
}
);
And in my redis GUI, it shows No keys to display., so it didn't create the key either.
What am I missing here ?
EDIT : I just realized that instead of connecting to the distant server, I was connecting to my local one somehow.
Then, it seems that it doesn't connect to the distant.
EDIT2 : I forgot the async connect function, this is why it didn't work !

how to create interactive ssh terminal and enter commands from the browser using Node JS in a Meteor app

I'm trying to create a web page where the user can authenticate to a remote server via ssh with username/password, and then interact with the remote server.
I'm not looking to create a full interactive terminal: the app server will execute a limited set of commands based on user input and then pass the responses back to the browser.
Different users should interact with different ssh sessions.
My app is built in Meteor 1.8.1, so the back end runs under Node JS, version 9.16.0. It's deployed to Ubuntu using Phusion Passenger.
I have looked at several packages that can create an interactive ssh session but I am missing something basic about how to use them.
For example https://github.com/mscdex/ssh2#start-an-interactive-shell-session
The example shows this code:
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
conn.shell(function(err, stream) {
if (err) throw err;
stream.on('close', function() {
console.log('Stream :: close');
conn.end();
}).on('data', function(data) {
console.log('OUTPUT: ' + data);
});
stream.end('ls -l\nexit\n');
});
}).connect({
host: '192.168.100.100',
port: 22,
username: 'frylock',
privateKey: require('fs').readFileSync('/here/is/my/key')
});
This example connects to the remote server, executes a command 'ls' and then closes the session. It isn't 'interactive' in the sense I'm looking for. What I can't see is how to keep the session alive and send a new command?
This example of a complete terminal looks like overkill for my needs, and I won't be using Docker.
This example uses socket.io and I'm not sure how that would interact with my Meteor app? I'm currently using Meteor methods and publications to pass information between client and server, so I'd expect to need a "Meteor-type" solution using the Meteor infrastructure?
child_process.spawn works but will only send a single command, it doesn't maintain a session.
I know other people have asked similar questions but I don't see a solution for my particular case. Thank you for any help.
I got this working by following these instructions for creating an interactive terminal in the browser and these instructions for using socket.io with Meteor.
Both sets of instructions needed some updating due to changes in packages:
meteor-node-stubs now uses stream-http instead of http-browserify
https://github.com/meteor/node-stubs/issues/14 so don't use the hack for socket
xterm addons (fit) are now separate packages
xterm API has changed, use term.onData(...) instead of term.on('data'...)
I used these packages:
ssh2
xterm
xterm-addon-fit
socket.io
socket.io-client
and also had to uninstall meteor-mode-stubs and reinstall it to get a recent version that doesn't rely on the Buffer polyfill.
Here's my code.
Front end:
myterminal.html
<template name="myterminal">
<div id="terminal-container"></div>
</template>
myterminal.js
import { Template } from 'meteor/templating';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import './xterm.css'; // copy of node_modules/xterm/css/xterm.css
// xterm css is not imported:
// https://github.com/xtermjs/xterm.js/issues/1418
// This is a problem in Meteor because Webpack won't import files from node_modules: https://github.com/meteor/meteor-feature-requests/issues/278
const io = require('socket.io-client');
Template.fileExplorer.onRendered(function () {
// Socket io client
const PORT = 8080;
const terminalContainer = document.getElementById('terminal-container');
const term = new Terminal({ 'cursorBlink': true });
const fitAddon = new FitAddon();
term.loadAddon(fitAddon);
term.open(terminalContainer);
fitAddon.fit();
const socket = io(`http://localhost:${PORT}`);
socket.on('connect', () => {
console.log('socket connected');
term.write('\r\n*** Connected to backend***\r\n');
// Browser -> Backend
term.onData((data) => {
socket.emit('data', data);
});
// Backend -> Browser
socket.on('data', (data) => {
term.write(data);
});
socket.on('disconnect', () => {
term.write('\r\n*** Disconnected from backend***\r\n');
});
});
});
Server:
server/main.js
const server = require('http').createServer();
// https://github.com/mscdex/ssh2
const io = require('socket.io')(server);
const SSHClient = require('ssh2').Client;
Meteor.startup(() => {
io.on('connection', (socket) => {
const conn = new SSHClient();
conn.on('ready', () => {
console.log('*** ready');
socket.emit('data', '\r\n*** SSH CONNECTION ESTABLISHED ***\r\n');
conn.shell((err, stream) => {
if (err) {
return socket.emit('data', `\r\n*** SSH SHELL ERROR: ' ${err.message} ***\r\n`);
}
socket.on('data', (data) => {
stream.write(data);
});
stream.on('data', (d) => {
socket.emit('data', d.toString('binary'));
}).on('close', () => {
conn.end();
});
});
}).on('close', () => {
socket.emit('data', '\r\n*** SSH CONNECTION CLOSED ***\r\n');
}).on('error', (err) => {
socket.emit('data', `\r\n*** SSH CONNECTION ERROR: ${err.message} ***\r\n`);
}).connect({
'host': process.env.URL,
'username': process.env.USERNAME,
'agent': process.env.SSH_AUTH_SOCK, // for server which uses private / public key
// in my setup, already has working value /run/user/1000/keyring/ssh
});
});
server.listen(8080);
});
Note that I am connecting from a machine that has ssh access via public key to the remote server. You may need different credentials depending on your setup. The environment variables are loaded from a file at Meteor runtime.

Nodemon start only webserver (not db server) in Node

I am using nodemon to restart my node application when ever changes were made. My problem is every time both web-server and DB server are restarting after code changes were made. I am using Oracle DB. below is my app.js code:
const webServer = require('./services/web-server.js');
const database = require('./services/database.js');
const dbConfig = require('./config/database.js');
const defaultThreadPoolSize = 4;
async function startup() {
console.log('Starting application');
//Initializing web server module
try {
console.log('Initializing web server module');
await webServer.initialize();
} catch (err) {
console.error(err);
process.exit(1); // Non-zero failure code
}
//Initializing the Oracle DB
try {
console.log('Initializing database module');
await database.initialize();
} catch (err) {
console.error(err);
process.exit(1); // Non-zero failure code
}
//Stopping Oracle DB
/*try {
console.log('Closing database module');
await database.close();
} catch (err) {
console.log('Encountered error', e);
err = err || e;
}*/
}
startup();
services/web-server.js, I am creating a http server like below:
httpServer = http.createServer(app);
services/database.js, I am creating a pool for Oracle:
const pool = await oracledb.createPool(dbConfig.hrPool);
Please suggest me how can I restart only web-server with nodemon? I don't want DB connection to restart always...
Your database server doesn't restart every time you restart your node application.
What's happening is your node is recreating the connection to the database every time it's restarted, which is normal and can't be avoided.
You can check this by connecting to your Oracle server and doing queries on it while your node application is stopped.

Connecting RabbitMq to an TCP/IP server

I am using RabbitMq as a Queueing Mechanism for my message handling.
So far, all the queues are generating fine on the localhost but when I moved on to the server. It started showing
connection timeout error
This is my connecion string.
var amqp = require('amqp');
var connection = amqp.createConnection({
url: 'amqp://username:passwprd#server-IP:5672/'
})
connection.on('error',(err)=>{
console.log(err);
});
var options = { autoDelete:false,
durable:false,
expiration:'20000',
};
connection.on('ready',()=>{
connection.queue('queueName',options,(queue)=>{
queue.bind('#');
queue.subscribe({ack:true},message =>{
console.log(message);
});
});
});
Can anyone tell me what i am doing wrong here
I have launched the Ubuntu EC2 instance and using putty utility I have installed rabbitq on the same.
To know the status of rabbitmq service, you can type service rabbitmq-server status
You can refer documentation from Rabbitmq node Rabbitmq
I have tried with below code on aws instance and its working
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
console.log(conn);
});
Output
Actually I get what you are asking
Just type this code
var connection = amqp.createConnection({
url: 'amqp://localhost'
})
on your server side code and the project will run fine as it is running on my server.
You don't need to specify the username and password or your server-ip.

Node module; Mongod not returning promise at all?

I'm using mongod
module to start a MongoDB server for my node.js app.
I'm quite new to programming and after some googling I think it is the best I could come up with.
In mongod documentation, Mongod#open() returns a promise, which I believe I consume correctly.
However, my code never gets to the point where the client connects and I can't spot what I did wrong.
Here's my code:
const
Mongod = require('mongod'),
MongoClient = require('mongodb').MongoClient,
config = require('./config');
const
mongoURI = 'mongodb://localhost:27017/' + config.db.table_name,
server = new Mongod({
conf: config.db.path_to_cfg
});
const connectToDB = (uri) => {
return MongoClient.connect(uri);
};
//If mongod service is not running, create a new server and connect
console.log(server.isRunning); //Just to verify
if (!server.isRunning) {
console.log('dbConnect:: Starting MongoDB server...');
server.open()
.then(() => {
connectToDB(mongoURI)
.then((db) => {
console.log(db)
})
.catch((err) => {
console.log('Error: ' + err)
})
})
} else {
connectToDB(mongoURI)
.then((db) => {
console.log(db)
})
.catch((err) => {
console.log('Error: ' + err)
})
}
dbConnect:: Starting MongoDB server... logs in the console, the server runs successfully, since I can connect from windows CLI and query the database, but .then() is not executed and db is not logged.
I have tested the else block it is working as it should, (which also means my config module is correct) logging the db object when I have previously started the server from CLI.

Resources