JS: Node.js and Socket.io - globals and architecture - javascript

Dear all,
Im working with JS for some weeks and now I need a bit of clarification. I have read a lot of sources and a lot of Q&A also in here and this is what I learned so far.
Everything below is in connection with Node.js and Socket.io
Use of globals in Node.js "can" be done, but is not best practice, terms: DONT DO IT!
With Sockets, everything is treated per socket call, meaning there is hardly a memory of previous call. Call gets in, and gets served, so no "kept" variables.
Ok I build up some chat example, multiple users - all get served with broadcast but no private messages for example.
Fairly simple and fairly ok. But now I am stuck in my mind and cant wrap my head around.
Lets say:
I need to act on the request
Like a request: "To all users whose name is BRIAN"
In my head I imagined:
1.
Custom object USER - defined globally on Node.js
function User(socket) {
this.Name;
this.socket = socket; }
2.
Than hold an ARRAY of these globally
users = [];
and on newConnection, create a new User, pass on its socket and store in the array for further action with
users.push(new User(socket));
3.
And on a Socket.io request that wants to contact all BRIANs do something like
for (var i = 0; i < users.length; i++) {
if(user[i].Name == "BRIAN") {
// Emit to user[i].socket
}}
But after trying and erroring, debugging, googling and reading apparently this is NOT how something like this should be done and somehow I cant find the right way to do it, or at least see / understand it. can you please help me, point me into a good direction or propose a best practice here? That would be awesome :-)
Note:
I dont want to store the data in a DB (that is next step) I want to work on the fly.
Thank you very much for your inputs
Oliver

first of all, please don't put users in a global variable, better put it in a module and require it elsewhere whenever needed. you can do it like this:
users.js
var users = {
_list : {}
};
users.create = function(data){
this._list[data.id] = data;
}
users.get = function(user_id){
return this._list[user_id];
};
users.getAll = function(){
return this._list;
};
module.exports = users;
and somewhere where in your implementation
var users = require('users');
For your problem where you want to send to all users with name "BRIAN",
i can see that you can do this good in 2 ways.
First.
When user is connected to socketio server, let the user join a socketio room using his/her name.
so it will look like this:
var custom_namespace = io.of('/custom_namespace');
custom_namespace.on('connection', function(client_socket){
//assuming here is where you send data from frontend to server
client_socket.on('user_data', function(data){
//assuming you have sent a valid object with a parameter "name", let the client socket join the room
if(data != undefined){
client_socket.join(data.name); //here is the trick
}
});
});
now, if you want to send to all people with name "BRIAN", you can achieve it by doing this
io.of('/custom_namespace').broadcast.to('BRIAN').emit('some_event',some_data);
Second.
By saving the data on the module users and filter it using lodash library
sample code
var _lodash = require('lodash');
var Users = require('users');
var all_users = Users.getAll();
var socket_ids = [];
var users_with_name_brian = _lodash.filter(all_users, { name : "BRIAN" });
users_with_name_brian.forEach(function(user){
socket_ids.push(user.name);
});
now instead of emitting it one by one per iteration, you can do it like this in socketio
io.of('/custom_namespace').broadcast.to(socket_ids).emit('some_event',some_data);
Here is the link for lodash documentation
I hope this helps.

Related

Is it possible to define a global variable across an entire Node Express server?

I'm creating a Node/Express application that is dependent on data that is fetched from an API. The issue is that my application has to fetch quite often from the API and do some intensive calculations every time, so I want to cache results from both the API and the intensive calculations.
I already wrote out some very rudimentary caching functionality (i.e. writing values into a text file), but I'm curious if there's a way to keep the values in memory in some sort of object for easy access across the whole application (e.g. across multiple files) instead of having a util function to read in the values from a text file every time.
For example, maybe I have a paradigm that looks like this:
When the server first initializes, I fetch for the data, and create some sort of cached value:
const data = await api.get('colors');
const map = {
red: data.red,
blue: data.blue,
green: data.green,
}
In one of my controllers, I want to access map and be able to use the values. So maybe I'd be able to do something like this:
import { map } from './utils/mapManager.js'
const getGreenValue = (req, res) => {
res.send(map.green);
}
What is the best way to design a system that is trying to achieve this behavior?
Thanks in advance, and happy to answer any clarifying questions.
You can use expressjs app.locals property.
To write data:
app.locals.map = map;
To read data:
let map = app.locals.map;
Docs:
https://expressjs.com/en/api.html#app.locals
Please refer this doc -> Global
You can use global object in this case.
console.log(globalString); // Output: "This can be accessed anywhere!"
globalString = "Check me out now";
console.log(globalString); // Output: "Check me out now"
globalString = undefined;
console.log(globalString); // Output: undefined```
I suggest you to try express specific app.locals or res.locals.
Say you have an app initialized like
const app = express();
const data = await api.get('colors');
const map = {
red: data.red,
blue: data.blue,
green: data.green,
}
//Assign it to locals
app.locals.map = map;
Now the map is available throughout the lifetime of application and you can access the same in views just by calling the variable map.
res.locals does the same thing but it is available only until the lifecycle of request. Say, you want to send a particular color based on the request. You can do it like this.
app.use(function(req, res, next){
if(req.params.id==='admin')
res.locals.color = req.app.locals.map.red;
else
res.locals.color = req.app.locals.map.blue;
next();
});
With the above middleware, you will be able to have a variable color that is available to all the succeeding middlewares and the final view. You can access the same in view using color. This is persistent till the lifetime of that request.

Keep Reference to Connected Sockets Per User

I have a (for clarity's sake) a chat.
Users can login, write messages, and the others will see [name]:[message].
I don't want to send the user's name and ID every time I write socket.emit('say', message); because that's redundant, so what I'm doing on the server is like so:
var io = require("socket.io").listen(server),
sockets = {};
io.sockets.on('connection', function (socket){
socket.on('savePersonToSocket', function(user){
socket.user = user;
sockets[user.id] = socket;
}
socket.on('doSomething', doSomething);
});
// must be outside of 'connection' since I have TONS of these in a much more
//complex structure and I don't want to create them all per user connection.
function doSomething(){
...
sockets[userID].emit('foo'); // how to get the userID at this point?
...
}
So, how would I get the userID at that point?
Notes:
For each user that logs in and connects with their Facebook account, the client will tell the server to save the person's name and ID.
I thought of doing it with a cookie that saves the user's name and ID, and the server would know which user it is by reading the cookie, but that's a bit of an ugly solution: it's redundant to send that information every time.
I could also hijack the 'on' function (somehow) and add functionality that will know which user it is, because all the 'on' listeners must reside inside the 'connection' listener anyway.
What is probably happening is that a user connects to your chat application, so here
io.sockets.on('connection', function (s){
socket = s; // cache for later usage
you assign "his socket" to the variable socket which is unbound from the context, it is on its own. Now let's say a second user arrives, socket gets reassigned with the second of the second user, but if you get a savePersonToSocket event then socket will be used, which is the same for everyone and more in detail it is related to the last user that connected.
There is no need for you to keep a reference to s, you will probably have to deal with that when you will have a lot of users connecting to your application, but the solution will be very different from your approach.
EDIT: another way could be by mapping user ids and sockets:
// this assumes that the name of a user is unique
var userSocket = {};
io.sockets.on('connection', function (s){
socket.on('savePersonToSocket', function(user){
userSocket[user.name] = s;
}
So basically you are saying that you don't want to pass to doSomething event payload the userID? Because that seems like a valid solution for me, it's not redudant, it's the simplest way to let the server know about what user it is dealing with. Other solutions might be more elegant, but I doubt they are as simple and as easily maintainable as this one.
If you can save sockets under one account can sit a few people, for example, such a system would be:
var socketsList
io.sockets.on('connection', function (socket) {
socket.on('auth', function (data) {
// request to database
if ( ! (UNIQUE_ID in socketsList)) {
socketsList[UNIQUE_ID] = {};
socketsList[UNIQUE_ID][socket.id] = socket;
});
});
// ONEXIT - BROWSER EVENT
var onExit = function () { socket.emit('exit', { UNIQUE_ID : 123}) }
window.onunload = onExit;
//
socket.on('exit', function (data) {
delete socketsList[UNIQUE_ID][socket.id]
})
Correct me if I'm wrong
you can't save the connection literally, it is changing quite frequently.
however you can save the socket.id. you have io.sockets.sockets[socketid]
this is probably what you are looking for.
you can save array of ids and names and reuse them if you like.
(be aware that process related variables are make it not so scaleable. but you can don't care for it the long first time :)
also you have socket related store object socket.handshake
socket.handshake.somevariable=simple_value_a_string_or_a_number
Theoretically, something as ugly as this should work, but the usage of apply is bad IMHO, and the lack of simple function pointers makes the code uglier
var io = require("socket.io").listen(server);
io.sockets.on('connection', function (socket){
socket.on('savePersonToSocket', function(user){
socket.user = usr;
}
// No more simple function pointers....
socket.on('doSomething', function(){
// pass the Socket as the scope
doSomething.apply(socket, arguments);
});
});
function doSomething(){
...
this.emit('foo');
...
}
how about this:
var io = require("socket.io").listen(server),
sockets = {};
io.sockets.on('connection', function (socket){
socket.on('savePersonToSocket', function(user){
socket.user = user;
sockets[user.id] = socket;
}
socket.on('doSomething', function() { doSomething(socket.user) });
});
// must be outside of 'connection' since I have TONS of these in a much more
//complex structure and I don't want to create them all per user connection.
function doSomething(user){
...
sockets[user.id].emit('foo');
...
}
I know it has been a long time since you asked this, but Just 4 ago I published a module for node js, express and socket.io which manages that exactly thing you wanted. Check the Usage and Example, I hope you will find this module helpful!
You can install it via NPM socket.io.users This is a node js module for socket.io applications. One user per client.
You can loook some of the usage code on this answer

Using an object property as a function and as an object simultaneously

I was wondering if there is a way to declare an object property as a function, but also as an object, at the same time.
I have a JavaScript program that provides a simple API that sends AJAX requests to a server. My goal is trying to make this API as simple and human-readable as possible.
Basically, I'd like to make it possible to do this:
var app = new App();
app.get.client(123) // Get client ID 123
app.get.client.list() // Get an array of all clients
app.login('username', 'password') // Send credentials to log as username/password
app.login.as('John') // Login using credentials stored in a server-side constant
I doubt that's even possible as I've never anything like it, but I can't think of a more clear and human-readable way to lay out methods. Sure would be nice!
A function’s an object too!
app.get.client = function(id) {
// Get client by ID
};
app.get.client.list = function() {
// List them
};
works as you’d expect.
Personally, though, I’d find:
app.clients.byId(123)
app.clients
app.login('username', 'password')
app.loginAs('John')
more readable.

Node.JS Socket.IO sending packets to specific connection IDs

I have been searching for this particular problem for the past week, and since I couldn't find any information on the subject(that wasnt outdated), I just decided to work on other things. But now I am at the point where I need to be able to send data(that I constructed) to specific clients using their ID who are connected to my server using node.js and socket.io. I already store the ID in an object for each new connection. What I need to know is a way to just send it to a connection ID I choose.
Something like: function send(data, client.id) {};
I am using an http server, not TCP.
Is this possible?
edit:
server = http_socket.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(respcont);
client_ip_address = req.header('x-forwarded-for');
});
socket = io.listen(1337); // listen
//=============================================
// Socket event loop
//=============================================
socket.on ('connection', function (client_connect) {
var client_info = new client_connection_info(); // this just holds information
client_info.addNode(client_connect.id, client_connect.remoteAddress, 1); // 1 = trying to connet
var a = client_info.getNode(client_connect.id,null,null).socket_id; // an object holding the information. this function just gets the socket_id
client_connect.socket(a).emit('message', 'hi');
client_connect.on('message', function (data) {
});
client_connect.on ('disconnect', function () {
);
});
solution: I figured it out by just experimenting... What you have todo is make sure you store the SOCKET, not the socket.id (like i was doing) and use that instead.
client_info.addNode(client_connect.id, client_connect.remoteAddress, client_connect, 1)
var a = client_info.getNode(client_connect.id,null,null,null).socket;
a.emit('message', 'hi');
If you need to do this, the easiest thing to do is to build and maintain a global associative array that maps ids to connections: you can then look up the appropriate connection whenever you need and just use the regular send functions. You'll need some logic to remove connections from the array, but that shouldn't be too painful.
Yes, it is possible.
io.sockets.socket(id).emit('message', 'data');
Your solution has one major drawback: scaling. What will you do when your app needs more the one machine? Using internal IDs also could be difficult. I advice using external IDs (like usernames).
Similarly to this post I advice using rooms (together with Redis to allow scaling). Add private room for every new socket (basing on user's name for example). The code may look like this:
socket.join('priv/' + name);
io.sockets.in('priv/' + name).emit('message', { msg: 'hello world!' });
This solution allows multiple machines to emit events to any user. Also it is quite simple and elegant in my opinion.

why should i declare handle["/"] at request handling object in http server with node.js?

I have encountered with this nice tutorial to node.js.
There is some example there :
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);
Why do I need handle["/"] what is it good for?
he says there :
As you can see, it's really simple to map different URLs to the same request handler: by adding a key/value pair of "/" and requestHandlers.start, we can express in a nice and clean way that not only requests to /start, but also requests to / shall be handled by the start handler.
why that solve any problem and what is the problem at all?
The function binded to handle["/"] will be called when somebody visits yoursite.com, and handle["/start"] will be called for visits to yoursite.com/start
The author is trying to build a server that will display different things based on routes (urls).

Categories

Resources