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.
Related
I'm trying to do a simple command line database transformation with node.js and sequelize. I've simplified my errant code down to the following, but it never returns:
// Set up database connection and models
var models = require('../models_sequelize');
models.User.findOne()
.then(a => {
console.log(a.name);
});
I get a name printed, but then the script hangs. What is wrong? How do I debug this to see what's stuck? I get the impression that there's an orphan promise that's not being fulfilled, but I don't understand where or why. I must be missing something obvious.
If I run the same interactively from the node console, it returns fine.
Sirko's comment re: close() gave me something to go on. I can stop the hanging with the following code:
var models = require('../models_sequelize');
models.User.findOne()
.then(a => {
console.log(a.name);
models.sequelize.close();
})
Alternatively, this seems to work too as I guess it's doing exactly the same thing:
var models = require('../models_sequelize');
models.User.findOne()
.then(a => {
console.log(a.name);
})
.finally(() => {
models.sequelize.close();
});
I also found something about connection pooling timeouts, but I don't think that affects my simple use case. I imagine it'll come into play in more complicated examples.
Would still like to find a good reference as to why this is necessary rather than just my guess.
In refluxjs I'm not sure what .listen() does. From my understanding, it has the same concepts as nodejs eventemitter but reflux wraps in its own way. I can't seem to find documentation on this anywhere. Maybe I missed it. I would like to find .listen() in the source code or documentation so I know exactly how refluxjs uses it.
Did you try the README? There's a whole section on it: Listening to changes in data store.
Listening to changes in data store
In your component, register to listen to changes in your data store
like this:
// Fairly simple view component that outputs to console
function ConsoleComponent() {
// Registers a console logging callback to the statusStore updates
statusStore.listen(function(status) {
console.log('status: ', status);
});
};
var consoleComponent = new ConsoleComponent();
Invoke actions as if they were functions:
statusUpdate(true);
statusUpdate(false);
With the setup above this will output the following in the console:
status: ONLINE
status: OFFLINE
And yes, its semantics are pretty much like EventEmitter; it uses eventemitter3 under the hood. listen itself is defined in PublisherMethods.js.
I'm about to start coding a chat bot. However, I plan on running more than one, using a wrapper to communicate and restart them. I have done this in the past with child_process.fork(), but it was incredibly inefficient. I've looked into spawn and cluster as well, but they all seem to focus on running the same thing, not unique bots. As for plugins, I've looked into fleet, forkfriend, and workerfarm, but none seem to fit my needs.
Is there any plugin or way I'm not seeing to help me do this? Or am I just going to have o wing it again?
You can have as many chat bots as you wish in a single process. The rule of thumb in Node.js is using one process per processor core since Node has slightly different multithreading model you might got used to.
Assuming you still need some multithreading on top of this, here is a couple of node modules you might find fitting your needs:
node-webworker-threads, dnode.
UPDATE:
Now I see what you need. There is a nice example in Node.js docs, which I saw recently. I just copy & paste it here:
var normal = require('child_process').fork('child.js', ['normal']);
var special = require('child_process').fork('child.js', ['special']);
// Open up the server and send sockets to child
var server = require('net').createServer();
server.on('connection', function (socket) {
// if this is a VIP
if (socket.remoteAddress === '74.125.127.100') {
special.send('socket', socket);
return;
}
// just the usual dudes
normal.send('socket', socket);
});
server.listen(1337);
child.js looks like this:
process.on('message', function(m, socket) {
if (m === 'socket') {
socket.end('You were handled as a ' + process.argv[2] + ' person');
}
});
I believe it's pretty much what you need. Launch several processes with different configs (if number of configs is relatively low) and pass socket to a particular one from master process.
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.
I'm using node-redis. In code like this:
var store = require('redis').createClient();
store.watch('some:key');
store.get('some:key', function (err, results) {
var multi = store.multi();
// COMPUTE SOMETHING WITH results
multi.set('something:or:other', 25);
multi.exec(checkAllIsWell);
});
Should lines 1-2 read
store.watch('some:key', function (err, alwaysok) {
store.get('some:key', function (err, result) {
or will watch always have immediate effect?
EDIT: To reframe the question a little, is sequence guaranteed on seqential calls on the same Redis client? Or could the WATCH happen after the GET?
Having reframed my question, I realize that it must surely be sequence-preserving, and I'm actually duplicating this question: Are Redis updates synchronous?
So the answer is surely that I don't need to wait for WATCH to call back and my original code is OK.
Sorry to noise up the web, folks!
Watch always returns OK. http://redis.io/commands/watch
It is useful only if later you use MULTI/EXEC, to check the EXEC return value.
For more information about Redis transactions, visit http://redis.io/topics/transactions