AEDES SERVER DOES NOT CONNECT TO CLIENT - server

I want to make a simple client server example with visual studio code. For my mqtt client instance, mosca didn't work. So I created a server with aedes. However, it is not possible to connect to client.js at the moment. I'm sure it's missing on the server side, but I'm not sure how to fix it. I'm very new to this. my codes are below.
Server;
const aedes = require('aedes')()
const server = require('net').createServer(aedes.handle)
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
const port = 1883
const wsPort = 3000
server.listen(port, function () {
console.log('server started and listening on port ', port)
})
ws.createServer({ server: httpServer }, aedes.handle)
httpServer.listen(wsPort, function () {
console.log('websocket server listening on port ', wsPort)
})
Client;
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.43.40:1883');
client.subscribe('new-user');
client.on('connect', function() {
console.log('connected!');
client.publish('new-user', 'Cansu-' + Math.ceil(Math.random() * 10));
});
client.on('message', function(topic, message) {
console.log(topic, ' : ', message.toString());
client.end();
});
Thank You!!!

Related

Snapshot return array

I've created a web socket server with Node.js to connect two Flutter apps. I can post a message to server but when I listen to it on WebApp i receive an array [79,101] instead message (Oi). How can I solve it?
Sink Message
void _sendMessage(data) {
widget.channel.sink.add('Oi');
}
Cliente Stream Builder
StreamBuilder(
stream: channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : 'Null');
},
)
Node.js Server
const WebSocket = require('ws');
// start the server and specify the port number
const port = 8080;
const wss = new WebSocket.Server({ port: port });
console.log(`[WebSocket] Starting WebSocket server on localhost:${port}`);
wss.on('connection', (ws, request) => {
const clientIp = request.sock.remoteAddress;
console.log(`[WebSocket] Client with IP ${clientIp} has connected`);
ws.send('Connected!');
// Broadcast aka send messages to all connected clients
ws.on('message', (message) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message); } })
console.log(`[WebSocket] Message ${message} was received`); });
});
This might not be the solution you are looking for but I would try and convert the response via String.fromCharCodesince the response might be ASCII characters to begin my way of debugging.

Creating LSP (Language Server Protocol) for multiple clients using RPC

I am trying to create a custom LSP.
My goal is to create one language server for both monaco editor (on web) and vscode extension.
Currently I use Node/IPC to connect the vscode-extension with the server.
and ws-jsonrpc to connect monaco editor with the server.
quoting from this article Extending a client with the language server protocol
:
There are multiple ways to use JSON-RPC, but you see these two ways in
most implementations:
Communication is done via the standard input / output, i.e., the command line interface
Communication is performed via TCP/IP, i.e., network messages similar to HTTP
I should be able to use JSON-RPC for both communication (internal communication between processes which is the vscode-extesion in my case , and external communication which is monaco-editor in my case)
Here is how I am launching the server for the moment:
For IPC communication:
const languageServer = new LanguageServer(createConnection(ProposedFeatures.all));
languageServer.start();
For WebSocket RPC:
import * as express from "express";
import * as ws from "ws";
import * as rpc from "vscode-ws-jsonrpc";
import * as url from "url";
import * as http from "http";
import * as net from "net";
const app = express();
const server = app.listen(3000);
const wss = new ws.Server({
noServer: true,
perMessageDeflate: false
});
function launch(socket : rpc.IWebSocket ){
const reader = new rpc.WebSocketMessageReader(socket);
const writer = new rpc.WebSocketMessageWriter(socket);
const languageServer = new LanguageServer(createConnection(reader, writer));
languageServer.start();
}
server.on('upgrade', (request: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
const pathname = request.url ? url.parse(request.url).pathname : undefined;
console.log("server on upgrade ", pathname);
if (pathname === '/sampleServer') {
wss.handleUpgrade(request, socket, head, (webSocket: any) => {
const socket: rpc.IWebSocket = {
send: (content: any) => webSocket.send(content, (error: any) => {
if (error) {
throw error;
}
}),
onMessage: (cb: any) => webSocket.on('message', cb),
onError: (cb: any) => webSocket.on('error', cb),
onClose: (cb: any) => webSocket.on('close', cb),
dispose: () => webSocket.close()
};
// launch the server when the web socket is opened
if (webSocket.readyState === webSocket.OPEN) {
launch(socket);
} else {
webSocket.on('open', () => {
launch(socket);
});
}
});
}
})
The server that you are using should not operate with multiple clients using it at the same time. It even says: "the protocol currently assumes that one server serves one tool.". Also, maybe read this article about your topic. **https://code.visualstudio.com/api/language-extensions/language-server-extension-guide **

React Native Parse LiveQuery error on socket

I am having trouble connecting to the LiveQuery server that I setup on the server side of my React Native project. I followed the instructions on the site verbatim, but can only manage to get 'error on socket' when I connect with numerous attempts by the server to reconnect.
Here is my server setup:
liveQuery: {
classNames: ['BekonRequest'],
}
var port = 1337;
server.listen(port, function() {
console.log('parse-server running on port ' + port); });
var parseLiveQueryServer = ParseServer.createLiveQueryServer(server);
server.listen(port, function() {
console.log('parse-server running on port ' + port);
});
var parseLiveQueryServer = ParseServer.createLiveQueryServer(server);
And my client side code:
let requestQuery = new Parse.Query('BekonRequest');
requestQuery.equalTo("username", "broncos#nfl.com");
let subscription = requestQuery.subscribe();
subscription.on('create', (requests) => {
console.log(requests);
});
Can anyone see why I am not able to connect successfully?

Net Module Nodester not Listening to Port

I have a basic node.js app that is designed to open up a connection between two clients and echo the input of one to the other.
var net = require("net");
console.log("Relay Started");
var id = 0;
var Socket = [];
relay = net.createServer(function(socket) {
socket.on('connect', function() {
console.log('Connected');
if(socket.id==null) {
socket.id = id;
Socket[id]=socket;
id++;
}
});
socket.on('data', function(data) {
data = data.toString()
if (socket.id==0) {
Socket[1].write(data);
} else if (socket.id==1) {
Socket[0].write(data);
}
console.log(socket);
console.log(data.toString());
});
})
relay.listen(process.env['app_port']||8080);
It works fine when run locally, however when I put it onto a Nodester development server, I am unable to connect by using telnet zapcs.nodester.com 18007 (it is hosted under the name zapcs, and the given port is 18007). The Relay Started is logged, but nothing after that, and no connection. Any ideas on why this would be?
~
you can not telnet zapcs.nodester.com 18007, you only can connect to zapcs.nodester.com:80 by http or websock, nodester will route your request to your app actual port (18007) on the host.
And check this: http://www.slideshare.net/cmatthieu/nodester-architecture-overview-roadmap-9382423

creating a tcp socket with net.createConnection(port, [host]) in node.js

Anyone here can give me a few pointers working with sockets in node.js?
can open a tcp connection say on 172.0.0.1 on port 8000 for example using net.createConnection(port, host)
var net = require('net'),
querystring = require('querystring'),
http = require('http'),
port = 8383,
host = 172.123.321.213,
path = /path/toService,
_post = '';
var server = http.createServer(function(req, res) {
if(req.method == 'POST') {
req.on('data', function(data) {
body+=data;
});
req.on('end', function() {
_post = querystring.parse(body);//parser post data
console.log(_post);
})
}
var socket = net.createConnection(port, host);
var socket = net.createConnection(port, host);
socket.on('error', function(error) {
send404(res, host, port);
})
socket.on('connect', function(connect) {
console.log('connection established');
res.writeHead(200, {'content-type' : 'text/html'});
res.write('<h3>200 OK:
Connection to host ' + host + ' established. Pid = ' + process.pid + '</h3>\n');
res.end();
var body = '';
socket._writeQueue.push(_post);
socket.write(_post);
console.log(socket);
socket.on('end', function() {
console.log('socket closing...')
})
})
socket.setKeepAlive(enable=true, 1000);
}).listen(8000);
send404 = function(res, host, port) {
res.writeHead(404, {'content-type': 'text/html'});
res.write('<h3>404 Can not establish connection to host: ' + host + ' on port: ' + port + '</h3>\n');
res.end();
}
But now I need to send my data to the path defined - if I add the path to host then try connection then connection will fail.
Any ideas?
Thanks in advance
Your "socket" object is just a plain TCP socket which is just a simple bidirectional communication channel. The HTTP methods you're trying to use (e.g. res.writeHead()) don't pertain, so you'll have to write the request manually. Try something like this:
var socket = net.createConnection(port, host);
console.log('Socket created.');
socket.on('data', function(data) {
// Log the response from the HTTP server.
console.log('RESPONSE: ' + data);
}).on('connect', function() {
// Manually write an HTTP request.
socket.write("GET / HTTP/1.0\r\n\r\n");
}).on('end', function() {
console.log('DONE');
});