How to connect meteor server to another tcp server via tcp socket - tcp

I want to create a meteor application that connect meteor server to another tcp server,
that is meteor server is client side and another tcp server is server side.
1.It like WebSocket of browser ,but I want use it on meteor server ,not on meteor client .
WebSocket using like :
var tcpServer = "ws://some.tcpServer.org/";
socket = new WebSocket(tcpServer,options);
socket.onopen = function(evt) { onOpen(evt) };
socket.onclose = function(evt) { onClose(evt) };
socket.onmessage = function(evt) { onMessage(evt) };
socket.onerror = function(evt) { onError(evt) };
2.Maybe as follow :
if (Meteor.isServer) {
Meteor.startup(function () {
var tcpServer = "ws://some.tcpServer.org/";
socket = new socketClient(tcpServer,options);
socket.onopen = function(evt) { onOpen(evt) };
socket.onclose = function(evt) { onClose(evt) };
socket.onmessage = function(evt) { onMessage(evt) };
socket.onerror = function(evt) { onError(evt) };
})
}
3.Or ...
var net = Npm.require('net');
net.createServer(function(socket){...} is for server side , not fit for my idea.
Maybe like net.createClient(function(tcpServer, options ){...} , but no this method.
I had been looking around but i do not see a method for Meteor. Can anyone give any pointers for me to start?
Thank a lot !

There are several problems with what you described.
WebSocket, which works over HTTP, and TCP are completely different protocols. You will not be able to open a WebSocket connection to a TCP server.
To have the Meteor server communicate with another server over TCP, you'll want to use Node's net API. To use this with Meteor, you'll need to use async callbacks in a way that is compatible with Fibers; see this blog post for an overview and https://stackoverflow.com/a/21542356/586086.

Here you have link to working plugin:
websocketify on atmosphere.com

Related

Web Socket Connection Failed on Azure Server

I have created web socket server using Asp .Net Web Api C# and its working fine on my local. But after hosting to Azure server it showing error below.
WebSocket connection to 'wss://mysite.azurewebsites.net/webapi/api/Chat' failed:
I have enabled the Web Socket Option on Azure App Configuration Section as well.
My Azure subscription is pay-as-you-go and App Service Plan is F1:Free.
Is there anything I have to change in settings?
Here is my Code :
client side:
$(document).ready(function() {
var url = 'wss://mysite.azurewebsites.net/webapi/api/Chat';
alert('Connecting to: ' + url);
ws = new WebSocket(url);
ws.onopen = function() {
alert('Connected');
$('#mySendBtn').click(function() {
ws.send($('#txtMessage').val());
$('#txtMessage').val('');
});
};
ws.onmessage = function(e) {
//some code
};
$('#btnLeave').click(function() {
ws.close();
});
ws.onclose = function() {
alert('Closed <br/>');
};
ws.onerror = function(e) {
alert('error');
};});
Server Side ref : https://www.codeproject.com/Articles/618032/Using-WebSocket-in-NET-4-5-Part-2

Meteor Admin Microservice via DDP

I am trying to build an Admin panel that will connect to another apps database and have the ability to change the data stored there.
var remote = DDP.connect('http://localhost:3000/');
var ServerAItems = new Mongo.Collection('items', { connection: remote });
Meteor.startup(function() {
console.log(remote);
remote.subscribe('smallBatchProducts', function(item){
});
console.log(ServerAItems.find().count(), 'test');
});
ServerAItems.find().count(); //returns 0
I have looked at Meteor: No docs from remote collection in template helper when connecting two apps via DDP and Connect two Meteor applications using DDP , but still can't figure out how to interact with the data and give the client access to it. The publication on localhost:3000 is smallBatchProducts.
I'm planning to have Flow Router handle the routing. Thanks
You should probably just put your console.log into the onReady callback.
Meteor.startup(function() {
remote.subscribe('smallBatchProducts', {
onReady: function(){
console.log(ServerAItems.find().count(), 'test');
},
onError: function(err) {}
});
});

socket.io emit specific transports

can socket.io emit in one transport style then in another?
socket.emit('my event',{data:'something'},{transports: ['websocket','xhr-polling']});
socket.emit('my event',{data:'somethingelse'},{transports: ['flashsocket']});
my reason is that i want to create a flex .swf with FlashSocket.IO that talks to my server app.js the same as my client.html javascript does.
I think you should use namespace and for your js client, connect to namespace a. For your flex client, connect to namespace b. This way you can manage your client easier and have much more clear picture on what you do with each group. You can also try something like this:
var io = require('socket.io').listen(80);
var mySocket;
var chat = io
.of('/chat')
.on('connection', function (socket) {
mySocket = socket;
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
});
});
var mySocket2;
var chat2 = io
.of('/chat2')
.on('connection', function (socket) {
mySocket2 = socket;
socket.emit('a message', {
that: 'only'
, '/chat2': 'will get'
});
});
function myEmit(channel, msg) {
if (channel == 'chat1')
mySocket.emit(msg);
else if (channel == 'chat2')
mySocket2.emit(msg)
}

Can I publish only Collections object in meteor?

On the introducing article of DDP, I read that anything can be published, but I've read somewhere ( for example in this Stackoverflow comment Publish arbitrary data and automatically update HTML ) that only Collections can be published.
So where is the truth? If we can publish other things than Collections, I would liek to see an example as I can't find one so far.
From the docs: http://docs.meteor.com/#meteor_publish
Publish functions can return a Collection.Cursor, in which case Meteor will publish that cursor's documents. You can also return an array of Collection.Cursors, in which case Meteor will publish all of the cursors.
So at the moment you can only return a Collection via a cursor (result of a Collection.find()).
To return other data you need to hack into the sockjs stream (the socket library meteor uses to communicate to the server). Be aware this does not guarantee compatibility with future versions of meteor. Sockjs is the library used for meteor to communicate between the server (the wire)
from Publish arbitrary data and automatically update HTML*
client side js
sc = new Meteor._Stream('/sockjs');
sc.on('message', function(payload) {
var msg = JSON.parse(payload);
Session.set('a_random_message', JSON.stringify(msg.data));
});
Template.hello.greeting = function () {
return Session.get('a_random_message');
};
server side js
ss = new Meteor._StreamServer();
ss.register(function (socket) {
var data = {socket: socket.id, connected: new Date()}
var msg = {msg: 'data', data: data};
// Send message to all sockets (which will be set in the Session a_random_message of the client
_.each(ss.all_sockets(), function(socket) {
socket.send(JSON.stringify(msg));
});
});
You can also look at Meteor Streams too. See below.
assume you have added meteor streams via atmosphere - mrt add streams
sc = new Meteor.Stream('hello');
if(Meteor.isServer) {
Meteor.setInterval(function() {
sc.emit('a_random_message', 'Random Message: ' + Random.id());
}, 2000);
Meteor.permissions.read(function() { return true });
}
if(Meteor.isClient) {
sc.on('a_random_message', function(message) {
Session.set('a_random_message', message);
});
Template.hello.greeting = function () {
return Session.get('a_random_message');
};
}

node.js HTTP request parsing (using net module)

Writing an HTTP simple server on top of Net node.js module, not using HTTP module.
I have a server listening at localhost:port with a socket opened.
socket.on('data', function(data){
clientMsg += data;
});
Once I type the address in the browser I can see the GET request is in clientMsg.
In order to return a response I use:
socket.on('close', function(){ something response generating here});
But this is not working well as it sends the response only once I click ESC or STOP in the browser.
So the question is, how can I know the browser finished sending the message and waits for a response, without closing the connection?
You would use the event connection instead of close.
Event: 'connection'
Also, this is the structure that is documented for such a server:
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});

Resources