mqtt paho ws not connected - connection timeout - javascript

I am very new to mqtt and recently is tasked to establish a connection using mqtt broker. I have this mqtt settings all set up, but unable to connect. I get this error every time the connect() function is called Connection failed: AMQJSC0001E Connect timed out. I'm running this over Xampp.
<html>
<head>
<script>
var options = {
timeout: 3,
userName: "myusername",
password: "mypassword",
onSuccess: function () {
alert("Connected");
console.log("mqtt connected");
//Connection succeeded; subscribe to our topic
client.subscribe(MQTTsubTopic, {qos: 0});
},
onFailure: function (message) {
alert("Connection failed: " + message.errorMessage);
console.log("connection failed");
}
};
//Create MQTT client here
var client = new Paho.MQTT.Client(MQTTbroker, MQTTport, "myclientid_" + parseInt(Math.random() * 100, 10));
client.onConnectionLost = function (responseObject) {
alert("connection lost: " + responseObject.errorMessage);
console.log("connection lost");
/* Connect to MQTT broker */
client.connect(options);
};
</script>
</head>
</html>
Here's my MQTT broker and port information which i have defined in another js file. '000.000.00.00' isn't a real broker. What could possibly be wrong in my connection establishment?
var MQTTbroker = '000.000.00.00';
var MQTTport = 9001;
On the other hand I attempted to connect with a sample broker broker.emqx.io with port 8083 and it's connected. I got the example from this link

Related

Node wss websocket client

I need to make a websocket connection to a wss address (a secure websocket connection). When I run the program I get Connect Error: Error: unable to verify the first certificate. Is there some way to set ssl certificates for the client so that I do not see this error?
Code:
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function (error)
{
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function (connection)
{
console.log('WebSocket Client Connected');
connection.on('error', function (error)
{
console.log("Connection Error2: " + error.toString());
});
});
client.connect('wss://dev-api.dev2.test.com:4900/ws/v1/4/Data/Value', 'echo-protocol');

Socket.io client cant connect to server

I have issue related to Socket.io connection to server.
Its working fine on my local, but on dev-server it cant connect.
My backend code look like this:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(8080);
console.log('CONNECTED');
io.on('connection', function (socket) {
var handshake = socket.handshake;
console.log(handshake);
console.log("new client connected");
var redisClient = redis.createClient();
redisClient.subscribe('notification');
redisClient.subscribe('rate');
redisClient.on("message", function(channel, message) {
console.log("New message: " + message + ". In channel: " + channel);
socket.emit(channel, message);
});
socket.on('disconnect', function() {
redisClient.quit();
});
});
And my client part like this:
var socket = io.connect('http://localhost:8080');
socket.on('notification', function (data) { console.log(data) }
The error that im facing is when socket.io client tries to send to URL "http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MD_W4lE" request and its failing, with error ERR_CONNECTION_REFUSED. The node server is runing i tested and also tried to change localhost to 127.0.0.1 and ipv4 address, i didnt helped.

Node js server not working

I am trying to make a node js server with ws module. But it's not working. It says upgrade required.
My server side code:
var WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('Msg received in server: %s ', message);
});
ws.send('Msg from server');
});
Client side code:
var WebSocket = require('ws');
var ws = new WebSocket('ws://localhost:8080/');
ws.on('open', function() {
ws.send('Msg from client');
});
ws.on('message', function(data, flags) {
console.log('Msg received in client: %s ', data);
});
Running npm update as an administrator should update the packages and solve your issue.
Here is a similar issue reported fixed by another user.
I didn't able to fix the problem. Then I used Socket.io. It works well.

How to echo all messages with "websockets/ws"

I'm using this websocket : https://github.com/websockets/ws and I want to send a message from one client to another. To begin with that, I wanted to echo the messages sent to the server, but the messages are just received by the client which has sent them. I know, that this works pretty easy with socket.io, but I have to use websockets/ws .
This is my server code:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message); //This echoes the message
});
ws.send('Connection Opened');
});
You have to iterate over all the clients connected to the socket server. Replace your ws.send(message); with
wss.clients.forEach(function(client) {
client.send(message);
});

node.js connect to server socket, dont know how to authenticate with password

I am building a desktop audio streaming application, which will be a client, i wish to connect to a server socket to stream audio, i have the host ip, the port and password. I am using node to connect:
var client = net.connect(serverDetails, function() { //'connect' listener
console.log('client created!');
});
client.on('connect', function() {
console.log('connected to: '+serverDetails.host+' port: '+serverDetails.port );
//client.write(serverDetails.password+'\r\n');
});
client.on('data', function(data) {
console.log('sending data to server!');
console.log(data.toString());
});
client.on('end', function() {
console.log('disconnected from server');
client.end();
});
at the moment it connects and the disconnects straight away, how can i pass the password so that the connection remains open? after which i can stream/send data to this socket.

Categories

Resources