how client/server communication works with meteor/nodejs - node.js

I'm learning about Meteor. Right now I'm trying to figure out how the client/server communication works, I have a file under [root]/server with all the information to connect to DB (user, password, host, port) also I have the following line of code to test this "communication"
var getUsers = function(name) {
return "Hi. I'm " + name;
};
and in my root folder I have:
console.log(getUsers("Juan!"));
however I'm getting "Uncaught ReferenceError: getUsers is not defined " error. so the question is, What I'm doing wrong????????
thanks in advance for any help or hint

Discover Meteor book have a great chapter about it:
Spanish: http://es.discovermeteor.com/chapters/publications-and-subscriptions/
English: https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/
I recommend you read the whole book, is very good.
Update:
This point of Meteor refence also explains very well the communication between methods, like server <> client communication.
http://docs.meteor.com/#meteor_call

Related

socket.io rooms difference between to and in

I'm trying to get myself acquainted with socket.io and node. https://socket.io/docs/rooms-and-namespaces/
This is my reference.
var socketIO = require('socket.io')(http);
socketIO.on('connection', function(socket) {
socket.join(data.room);})
socketIO.in(users[key].room).emit('newmsg', data);
socketIO.to(users[key].room).emit('newmsg', data);
Here the code with socketIO.in gives output whereas socketIO.to doesn't
But as per their documentation in and to should return the same o/p.
Someone please explain to me the critical difference b/w them.
Right in the socket.io doc:
namespace.in(room)
Synonym of namespace.to(room).
So, .to() and .in() are the same.
And, if you look in the code, you see this:
Namespace.prototype.to =
Namespace.prototype.in = function(name){
if (!~this.rooms.indexOf(name)) this.rooms.push(name);
return this;
};
So, both .to() and .in() run the exact same code so any difference you think you are seeing is not because of the difference between calling .to() or .in(). It must be due to something else. You'd have to show us a reproducible set of code that shows some difference for us to help you debug that.

socket.io - io.sockets.adapter object?

I'm experimenting with socket.io and trying to build a multi-room chat app. The guide I'm following is out of date using pre 1.0.0 socket.io.
I'm trying to find a list of connected clients in a given room. Googling around shows that I have to use the adapter. However, I cannot find the documentation for it anywhere. I searched for it in the git-hub doc but search didn't return any information on adapter. https://github.com/socketio/socket.io-client/blob/master/docs/API.md
Can someone point me in the right direction and where I can read more about adapter and associated methods on it? Also if you can provide the most up to date documentation for socket.io I'd greatly appreciate it. Thank you.
You can get a map of all rooms in the top level namespace like this:
io.nsps['/'].adapter.rooms
You can list the sockets in one of those rooms like this:
function getSocketsInRoom(room, namespace = '/') {
let room = io.nsps[namespace].adapter.rooms[room];
return room.sockets;
}
As best I can tell, this kind of stuff is simply not documented. I've only discovered things like this by examining how things are stored in the debugger. That may or may not mean it's subject to change in the future - I really have no idea.
sockets:
{ '2v8OmIS4qTGX61-YAAAC': true, '3YnScxOgpmAGhZWsAAAG': true },
length: 2 }
it gives u this output. So it basically gives you the clientId and whether it is connected or not and total number of clients connected to a specific room. When the code below is executed (in server side written in node.js) gives u the above output.There is currently two clients connected to the same room named "hello".
var clientsInRoom = io.sockets.adapter.rooms[room];
but when u write this code below and console log it
var clientsInRoom = io.sockets.adapter.rooms
when single client is connected it will console log this
{ '9mVAHSDwcwnqsF4aAAAA':
Room { sockets: { '9mVAHSDwcwnqsF4aAAAA': true }, length: 1 } }
this crazy '9mVAHSDwcwnqsF4aAAAA' literals is client id which is unique for each client

Upgrading NodeJS App

I hope this is not too much to ask.
Im new to this NodeJS tech, which I think is amazing, but Im not a really good programmer, I found this awesome example http://softwareas.com/video-sync-with-websocket-and-node/, but I cant make it work. I believe the code its outdated, and Im running the latest Node, I've been trying to fix it but I cant get near close.
I dont fully understand how websockets work, and is giving me a headache.
When I run the server it runs just fine.
var sys = require("util");
var ws = require('websocket-server');
var userCount = [];
var server = ws.createServer({
debug: true
});
server.addListener("connection", function(conn){
server.broadcast("userCount " + ++userCount);
conn.addListener("message", function(message){
server.broadcast(message);
});
});
server.addListener("close", function(conn){
server.broadcast("userCount " + --userCount);
});
server.listen(8000, "localhost");
function log(msg) {
sys.puts(+new Date + ' - ' + msg.toString());
};
But when I interact with the client (Log as a user) the server crashes and I get this error in the console, and I've no idea what it means.
http://i.stack.imgur.com/cLlga.png
I'm really new to this, and I don't understand D:, any help is really appreciated
That package 'websocket-server' is unmaintained for 4 years, you should consider using another one.
There have been several forks in github, https://github.com/miksago/node-websocket-server/network, but with few changes more, so they are almost same obsolete.
Trying the examples in the package, https://github.com/miksago/node-websocket-server/tree/master/test/manual, the echo-server.js (very similar to your code) doesn't work, server hangs (or appears to hang), with no traces. If you try chat-server.js it does work, but I don't know if it does what you need/want.
You can run those examples with command:
node test/manual/chat-server.js
Double check if there is a newer module that does the same :)

Interacting with app code in the node REPL

One of the pleasures of frameworks like Rails is being able to interact with models on the command line. Being very new to node.js, I often find myself pasting chunks of app code into the REPL to play with objects. It's dirty.
Is there a magic bullet that more experienced node developers use to get access to their app specific stuff from within the node prompt? Would a solution be to package up the whole app, or parts of the app, into modules to be require()d? I'm still living in one-big-ol'-file land, so pulling everything out is, while inevitable, a little daunting.
Thanks in advance for any helpful hints you can offer!
One-big-ol'-file land is actually a good place to be in for what you want to do. Nodejs can also require it's REPL in the code itself, which will save you copy and pasting.
Here is a simple example from one of my projects. Near the top of your file do something similar to this:
function _cb() {
console.log(arguments)
}
var repl = require("repl");
var context = repl.start("$ ").context;
context.cb = _cb;
Now just add to the context throughout your code. The _cb is a dummy callback to play with function calls that require one (and see what they'll return).
Seems like the REPL API has changed quite a bit, this code works for me:
var replServer = repl.start({
prompt: "node > ",
input: process.stdin,
output: process.stdout,
useGlobal: true
});
replServer.on('exit', function() {
console.log("REPL DONE");
});
You can also take a look at this answer https://stackoverflow.com/a/27536499/1936097. This code will automatically load a REPL if the file is run directly from node AND add all your declared methods and variables to the context automatically.

node.js http.connections

I am looking over node.js code and can't understand some of it. Please help me with it.
var http = require("http"),
server = http.createServer(function(req,res) {});
......
if(MaxUserCheck <1 ){
server.watcher.stop();
logmsg(level1, server.connections);
}
For the above code,
what is "watcher" and how to use it?
"server.connections" - what is this? and how to use it?
I have seen using server module as
server.on('request', function (req,res){};
...
server.listen(52273, function(){};
and I can understand as above, but using as "server.connections" can't understand and haven't seen it use like that.
I looked up on node.js manual but doesn't explain it.(http://nodejs.org/api/)
It seems that "server.connections" returns how many clients connected to our server..(Server uses Fugue for multi clients)
where can I find the usage of "server.connections" and "server.watcher"
Thank you.
These two syntaxes might have been used/required in other directories. If I export this module to a different file and say require('watcher') there , it will work.however I'm not sure what watcher is used but it could be webwatcher module.

Resources