I need to synchronize some of data immediately between some servers with main one.
So I think the best and easiest way is using WebSocket in NodeJS.I have some experienced with socket.io module,but it provide client to use in browser.I looked at engine.io ,it looks like socket.io too.
Is there any library to make WebSocket connection as client with out browser?
(or any alternative safe protocol for my situation?)
If you're going to be transferring data across servers, you aren't limited to using the HTTP protocol. Instead, you can use raw TCP sockets. This is how you'd make a listen server:
var net = require('net');
var server = net.createServer(function(socket) {
// do what you need
socket.write();
socket.end();
});
server.listen(8080);
And this is how you'd connect to it from another Node process:
var net = require('net');
var client = net.connect({port: 8080}, function() {
// we can send data back
client.write();
});
client.on('data', function(data) {
// receive data here
});
client.on('end', function() {
// we received a FIN packet
});
Related
I'm still a bit new to Sockets and NodeJS, I wanted to be able to communicate to a RoboMaster robot over the Plaintext protocol using NodeJS, not Python like what is explained in the documentation. I'm not sure how to do this using NodeJS and am a bit confused if my application socket is a client or server. I would preferably like to convert the example code in the docs to a NodeJS friendly version, but not sure how. I have looked into things like Socket.io, but I'm not sure if that is what I need to be using.
Any help would be appreciated.
Edit: I found this example, it looks quite similar to what I need but I am not sure.
It turns out that I can use the net module to communicate with the RoboMaster robot. Using the code from here this is what it looks like:
var net = require('net');
var HOST = '192.168.2.1';
var PORT = 40923;
var client = new net.Socket();
client.connect(PORT, HOST, function () {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('command;');
});
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function (data) {
console.log('DATA: ' + data);
// Close the client socket completely
client.destroy();
});
// Add a 'close' event handler for the client socket
client.on('close', function () {
console.log('Connection closed');
});
I have webrtc node server like as below.
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
// process HTTP request. Since we're writing just WebSockets server
// we don't have to implement anything.
});
server.listen(1337, function() { });
// create the server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
// This is the most important callback for us, we'll handle
// all messages from users here.
connection.on('message', function(message) {
if (message.type === 'utf8') {
// process WebSocket message
}
});
connection.on('close', function(connection) {
// close user connection
});
});
I wonder that which port range is used by node server. We can see one port in the code (1337). But i think node server uses one more port or port range because of video stream. How can i learn which ports are used by webrtc node server.
The Node.js server does not use any additional ports for media. It is a signalling server which only relays session information(SDP exchange, ICE, etc.) and does not relay any media.
If the media was to be relayed by anything, it would be a TURN server but that would be determined by your ICE server set up.
Now, if you are handling media in a peerconnection on the same server as you are signalling, you can grab the port that the media is being streamed to the peerconnection from the SDP.
I'm creating a socket server like this:
var net = require('net');
net.createServer(function (socket) {
//Run code here, to select where to redirect this client
}).listen(5000);
This would be my master server, where I decide where to redirect a client (e.g. I want all the clients that are coming from an specific area to connect to an specific server, but I want the master server to decide, because is not just about the geographic area, but some other data to analyse to decide where to redirect the client).
I want somehow to redirect the client to another Node.js server running on the same "localhost" but different PORT without loosing the socket connection, and without over-heating/saturating the "master server", I want it to be clean of all the connections that travel thru it to get to this other server.
What you're making is a TCP proxy. I've written a basic one below. The function getServerBasedOnAddress is where you would pick which server to proxy to. I have just chosen a random one here because I don't know what logic you require.
var net = require('net');
// The servers we will proxy to
var upstreamServerAddresses = [
{address: '127.0.0.1', port: '3000'},
{address: '127.0.0.1', port: '3001'},
{address: '127.0.0.1', port: '3002'},
];
// This is where you pick which server to proxy to
// for examples sake, I choose a random one
function getServerBasedOnAddress(address) {
return upstreamServerAddresses[Math.floor((Math.random() * 3))]
}
// Create the proxy server
net.createServer(function (socket) {
socket.on('data', function(data){
upstream = getServerBasedOnAddress(socket.remoteAddress);
net.connect(upstream.port, upstream.address, function(connection){
this.write(data);
});
})
}).listen(5000, function(){
console.log("Ready to proxy data");
});
// Create the upstream servers
for(var i = 0; i < upstreamServerAddresses.length; i++){
var upstream = upstreamServerAddresses[i];
net.createServer(function (socket) {
socket.on('data', function(data){
console.log("Received some data on " + upstream.address + ":" + upstream.port);
console.log(data);
});
}).listen(upstream.port);
}
As I say, this is a simplistic example. In real life you might want to make some changes, such as not connecting to clients on each packet but rather maintaining a persistent connection. You would probably also want to cover the case where an upstream server was unreachable.
You might also want to use something like the async library to ensure all the clients are up before starting your proxy server so no data is lost.
I have a http server to serve so many connections. This server will get data from internet, or from disk, cache to response to client. I want to create some workers to do some job using ZMQ.
ZMQ server will send data to ask worker do their job like insert to db, write to disk and most important send data back to client.
Server :
http.createServer(function(req, res) {
...
zmq_socket_server.send(message);
});
Worker :
zmq_socket.on('message', function(reply) {});
I don't know how to send data from workers to each client request .
Is it possible to do like that or any suggestion ?
You can use a simple request reply pattern using the REQ and RES sockets.
Server:
var socket = zmq.socket('req');
http.createServer(function(req, res) {
socket.send("send data");
socket.on('message',function(reply){
//worker sent back reply
});
});
Client:
var socket = zmq.socket('rep');
socket.on('message', function(data) {
socket.send("reply back here");
});
I have skipped out the obvious socket configuration part where you bind and connect to the socket address in each case, and I am assuming that you are using JustinTulloss's zmq npm module.
I'm trying to create a test using LearnBoost's socket.io and the node-websocket-client. Communication between the client and server work great. After all communication is done, I close both the client and the server. Yet the program hangs, waiting on some unknown callback. Two questions:
What is the following program waiting for?
Is there a tool for diagnosing outstanding callbacks in node programs?
var connect = require('connect'),
io = require('socket.io'),
WebSocket = require('websocket-client').WebSocket;
var port = 7111;
var server = connect.createServer();
var socket = io.listen(server);
socket.on('connection', function(client) {
client.send('Welcome!');
client.on('message', function(message) {
console.log(message);
});
client.on('disconnect', function() {
console.log('closing');
server.close();
});
});
server.listen(port, function() {
var ws = new WebSocket('ws://localhost:' + port + '/socket.io/websocket');
ws.onmessage = function(message) {
console.log(message.data);
};
setTimeout(function() {
ws.send('~m~3~m~Yo!');
ws.close();
}, 10);
});
EDIT: changed the variable name of the WebSocket to ws to avoid confusion
var socket = io.listen(server);
You've created a socket on a port. You've never closed it.
socket.server.close() closes your (socket.io) socket.
When in doubt read the socket.io github examples
socket.server === server It's the server you pass in, in the liste statement so it's closed. I'm not sure what it's waiting for.
Below a way to shutdown all the connections and be able to run multiple expresso tests (using socket.io and socket.io-client).
The solution is tricky and buggy but works on 0.8.5. The main problem is regarding the library to use websockets (node-websocket-client).
Currently, on socket.io, the OS contributors have patched the websocket client. So, we must do the same on our socket.io-client npm package to be able to use finishClose method on the socket client side. Socket.io-client uses the websocket library as npm package, so you must find the file (websocket.js) and substitute it with the same on socket.io.
Afterwards, you could use finishClose method to ensure the connections are closed and with some custom server/client socket settings, the tests will run correctly.
var io = require("socket.io").listen(port);
io.set('close timeout', .2);
io.set('client store expiration', .2);
var client = require("socket.io-client").connect( "http://localhost", { port: port , 'reconnect': false, 'force new connection': true});
client.on('connect', function() {
client.disconnect();
});
client.on('disconnect', function() {
client.socket.transport.websocket.finishClose();
io.server.close();
});
io.server.on('close', function() {
setTimeout( function() {
done();
}, 500);
});
Hope, somebody can help.
The program is waiting because socket.io (server) is still listening for incoming connections. I don't know of any way to stop listening.