Use MQTTNet Server with MQTT.js client - javascript

I have started an MQTT server just like this example.
This code is hosted in an ASP.Net Core 2.0 application but I have tried console application with no luck.
I have also setup a client using the same demo as above and it connects perfectly. Also an Android client connects fine. But I have placed a MQTT.js client webpage but it will not connect with chrome showing net::ERR_CONNECTION_REFUSED error.
I believe that the problem is with server not supporting web sockets. Because if I start my client with WS type it will not connect.
var options = new MqttClientOptions
{
Server = "localhost",
//ConnectionType = MqttConnectionType.Tcp // Connects
ConnectionType = MqttConnectionType.Ws // Does not connect
};
Now MQTT.js supports TCP as far this link is telling. But I don't seem to be able to work it.
This is the code in my page javascript:
var client = mqtt.connect('tcp://localhost') //Also did mqtt://localhost
client.on('connect', function () {
client.subscribe('myTopic')
client.publish('myTopic', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
I want to know how can I make javascript MQTT client to use TCP? (Any other js plugin maybe?) Or alternatively how can I enable WebSockets in MQTTNet.
Thanks for your help.

MQTT.js does support both native MQTT and MQTT over Websockets, but if you are embedding it in to a web app it can only use MQTT over Websockets because the browsers sandbox will not allow it to use arbitrary TCP connections.
As for enabling Websockets in the broker I can't see anything obvious in the code so you'll probably have to raise and issue against the github project to ask for details.

The next version of MQTTnet (2.5) will have support for WebSocket connections for the server via AspNetCore 2.0.

Related

RabbitMQ: Handshake Terminated by server (ACCESS-REFUSED)

I'm trying to send RabbitMQ messages from my host machine to a Minikube instance with a RabbitMQ cluster deployed.
When running my send script, I get hit with this error:
Handshake terminated by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - Login was refused
using authentication mechanism PLAIN. For details see the broker logfile.
In the broker logfiles I can see this line:
Error on AMQP connection <0.13226.0> (172.17.0.1:40157 -> 172.17.0.8:5672, state: starting):
PLAIN login refused: user 'rabbitmq-cluster-default-user' - invalid credentials
I'm sure I have the correct credentials since I got them directly from the RabbitMQ pod, following the official documentation (link).
My send script is below:
const amqp = require('amqplib/callback_api');
const cluster = "amqp://rabbitmq-cluster-default-user:dJhLl2aVF78Gn07g2yGoRuwjXSc6tT11#192.168.49.2:30861";
amqp.connect(cluster, function(error0, connection)
{
if (error0)
{
throw error0;
}
connection.createChannel(function(error1, channel)
{
if (error1)
{
throw error1;
}
const queue = "files";
var msg = {
name: "Hello World"
};
var msgJson = JSON.stringify(msg);
channel.assertQueue(queue, {
durable: false
});
channel.sendToQueue(queue, Buffer.from(msgJson));
});
});
I know the code works as I ran the exact same script for my localhost setup and it worked. The only thing I've changed is the URL (for the Minikube RabbitMQ service).
I've seen a few other posts that contain a similar issue but most solutions are about including the correct credentials in the URI, which I have done.
Any other ideas?
You can use port forwarding the rabbitMQ service to your local machine and use UI login and check the password with the UI given by the RabbitMQ itself.
kubectl port-forward svc/rabbitmq UI-PORT:UI-PORT (must be 15672)
then from a browser
localhost:15762
must be enough
For clearance you can check if you can login from the container itself. If the login from the container fails you can also check the yaml file or the helm chart you are using for login methods and credentials. Plain login may be disabled.
Another situation may be with the distrubution. When deploying RabbitMQ I try to use bitnami charts. I can suggest them.
If all these fails there is another way you can use. You can try to create another user with admin privileges to connect to RabbitMQ and then keep using it.
For more information, you can post container/pod logs for us to see.
Good day.

Try to connect to a server with Google Assistance App

I need to send data out from my google assistance app to a database. In order to do this, I've created a server that takes the data, packages it, and then sends it out. I have the hostname and port and it works in a normal javascript/node.js program but when I use it in my google assistant app nothing happens. I tried figuring out the problem and it looks like the code just isn't connecting. The code I'm using to send data to the server is as follows:
function sendData(app){
var net = require('net');
var message = {"test": 200};
var thisMessage = JSON.stringify(message);
var client = new net.Socket();
client.connect(<port>, '<hostname>', function() {
app.tell(JSON.stringify(client.address()));
console.log('Connected');
client.write(thisMessage);
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
return 0;
}
(NOTE: Port and hostname left out for privacy purposes)
This completely skips over the app.tell, leading me to believe the connection is never made. I know it works asynchronously with the server, however, I don't understand why it isn't connecting whatsoever.
I have tried it both in simulation and on my smartphone with sandbox on and off. Is there a better way to connect? Note that the server I'm connecting to is python-based.
The problem is likely that you're running it on Cloud Functions for Firebase which has a limit on outbound connections under their free "Spark" plan. With this plan, you can only connect to other Google services. This is usually a good way to start understanding how to handle Action requests, but has limitations. To access endpoints outside of Google, you need to upgrade to either their "Flame" fixed price plan or "Blaze" pay-as-you-go plan.
You do not, however, need to run on Google's servers or need to use node.js. All you need is a public HTTPS server with a valid SSL cert. If you are familiar with JSON, you can use any programming language to handle the request and response. If you are familiar with node.js, you just need a node.js server that can create Express request and response objects.

SignalR - connect to websocket service from javascript without SignalR library

I have a small SignalR project that I've started that right now all it does is receives a string and echo it back to all connected users.
What I'm wondering is, since SignalR open websockets on my server - how can I connect to the service using regular websockets javascript code? (I have a reason for wanting to do it that way without the SignalR library).
I've watched using chrome developer tools and I found that the address the browser is connecting to is:
ws://localhost:53675/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=YKgNxA6dhmtHya1srzPPIv6KFIYEjLTFOogXqcjPErFcKCmRdjvS2X6A2KmraW%2BrLnRUNf68gYPdOkOOgJ8yRcq4iCDm%2BbUyLejsr2NySNZBvLloWuMIAvgI6oC%2Fyt%2Ba&connectionData=%5B%7B%22name%22%3A%22ophirhubtest%22%7D%5D&tid=7
How do I generate the token?
Then, it seems that the messages going between the client and server are just regular json formatted text (which will be easy to mimic):
{"C":"d-9E7D682A-A,1|E,0|F,1|G,0","S":1,"M":[]}
{"H":"ophirhubtest","M":"Echo","A":["test"],"I":0}
{"C":"d-9E7D682A-A,2|E,0|F,1|G,0","M":[{"H":"ophirHubTest","M":"printEcho","A":["You said: test"]}]}
If I just try to connect than it connects but the connection is quickly closed. If I remove the token it closes immediately.
Is it possible to connect to the WS "manually" ?
Before you can connect to the server there is connection negotiation happening. This is when the server sends all the data needed to send and receive messages. Without connection negotiation you won't be able to connect to the server. Once you implement connection negotiation you will be probably half into implementing a SignalR client. I wrote a blog post describing SignalR protocol which should help you understand how things works underneath and why connecting to the server with your own websocket is not straightforward (or simply impossible if you don't follow the SignalR protocol).
EDIT
The ASP.NET Core version of SignalR now allows connecting to the server with bare webSocket.
I just want to add a that it is possible to connect to ASP.NET Core version of SignalR with websocket but you have to add the magic char 30 at the end of every call you make
const endChar = String.fromCharCode(30);
socket.send(`{"arguments":["arg1"],"streamIds":[],"target":"TestMethod","type":1}${endChar}`);
Great answers by Frédéric Thibault but there is one important thing missing. You need to send the protocol and the version directly after connecting. Otherwise you will get the error:
An unexpected error occurred during connection handshake.
Here is a full working example on how to use signalR with plain JavaScript and WebSockets:
let socket = new WebSocket("wss://your-url");
socket.onopen = function(e) {
console.log("[open] Connection established");
const endChar = String.fromCharCode(30);
// send the protocol & version
socket.send(`{"protocol":"json","version":1}${endChar}`);
};
socket.onmessage = function(event) {
console.log(`[message] Data received from server: ${event.data}`);
// parse server data
const serverData = event.data.substring(0, event.data.length - 1);
// after sending the protocol & version subscribe to your method(s)
if (serverData === "{}") {
const endChar = String.fromCharCode(30);
socket.send(`{"arguments":[],"invocationId":"0","target":"Your-Method","type":1}${endChar}`);
return;
}
// handle server messages
};
socket.onclose = function(event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
console.log('[close] Connection died');
}
};
socket.onerror = function(error) {
console.log(`[error] ${error.message}`);
};

How to work with WebSockets

I'm trying to use WebSockets in PHP and Javascript and I'm confused, it uses the protocol ws:// and wss:// and the server (hostgator) not enabled these protocols (I think!) And I can not connect, someone know if I need to enable these protocols on the server or how do I connect?
EDIT: It is possible to use websockets for streaming audio?
I tried connecting to another server I found in examples and got ...
Following code:
<script>
var connection = new WebSocket('ws://echo.websocket.org/')
connection.onopen = function(e) {
alert("Connected");
console.log("Connected");
};
connection.onclose = function(e) {
alert("Connection closed");
console.log("Connection closed");
};
</script>
I think this might help.
This is the programming assignment I made on PHP. Its a 2-way chat application which deploys a Client-Server on both the machines.
Client A talks to Server B
Client B talks to Server A
http://rishabhmehan.com/?attachment_id=255
This will be good for reference. It has the readme file in it, that can help you.
Also check this - http://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP

Websocket server on Azure with node.js

I'm trying to create a websocket server with Node.js to run on a Windows Azure. This should be simple, but I have run into problems I haven't been able to find solutions for anywhere.
This is my serverside code:
var ws = require('websocket.io')
, http = require('http').createServer().listen(process.env.PORT)
, server = ws.attach(http)
server.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('close', function () { });
});
How do I know which port I need to connect to?
My client code is:
var ws = new WebSocket("ws://socketiopuge.azurewebsites.net");
ws.onopen = function () {
alert("opened");
};
ws.onmessage = function(msg) {
alert(msg);
};
ws.onclose = function () {
alert("closed");
};
When I run the code I get an 501 error code, and the onclode event is fired. I believe the problem is that I need to specify a port number when I create the WebSocket. Can any of you point me in the right direction? Thanks!
About your question "How do I know which port I need to connect to", you would need to create an Input Endpoint and set proper PORT for it. Once you configured it, you will be using the same port in your code to bind and use.
Here is an example about Running socket.io on Windows Azure Web and Worker roles
If you host socket.io in a Windows Azure Web Role, please disable the WebSockets transport on the server because Windows Azure Web role runs on IIS7, web sockets are not supported on IIS7 yet. With Worker Role you dont need to worry about it and you can directly use Web Sockets.
If you're using the new Azure web sites, they do not support websockets yet. The same goes for all web sites hosted through IIS - node.js or not.
Your only way of currently supporting websockets in Azure is by using worker roles and supplying your own node.js executable.

Categories

Resources