meteor dictionary on server with client (session) ids as key - javascript

When a client logs in, i want to store inside a dictionary the ids of the clients / sessions as keys inside a dictionary mapped to some data (In case there is no need for a dictionary and meteor has a way to map a single variable with client specific data it would be even better).
I am not familiar with developing Meteor packages and Javascript at all, so my question is: Where do i put this dictionary so i can access it from everywhere on the server and where can i get the clients ids?
Wished behaviour:
1. Client logs in -> Server registers new client id
2. Client calls function on server
3. Inside the function the server gets the right data according to the clients id for further processing
Inside a package (https://github.com/steffow/meteor-accounts-saml) i set up a global variable but when i tried to access it from javascript, inside the imports/api directory, the global variable was undefined.

You could use the Presences package to store information about each logged in user, https://github.com/dburles/meteor-presence. It automatically creates unique ids for these.
These database records are available both in the client and server. You can either fork and extend this package to store more information, or create another database table to store your information.

Related

php: two client to share server user defined variable

GOAL: create kitchen and front desk can notify each other.
Q: how do i create php variable that can be read and set from kitchen and front desk. Let say the variable is vStatus. so the plan is when the front desk create order then vStatus = 'kitchen1', and when kitchen notify order is ready then vStatus='frontdesk1'.
on each other i create timer function to monitor the vstatus:
on front desk when vstatus='frontdesk1' then page is refreshed, and set vstatus=''. so the timer function on front desk side cycle continue until vstatus=frondesk1.
on kitchen when vstatus='kitchen1' the kitchen page is refreshed and set vstatus='' and timer function on kitchen side cycle until vstatus='kitchen1'
the hardware setup is like this:
front desk :apache web server, web browser
kitchen : web browser
You cannot share a PHP variable between separate requests - that would be a terrible thing from security point of view.
What you are looking for is a storage - some place outside of the php process that would persist your value even when the request processing (php execution) ends.
There are numerous options available for that, but here's a few most common and easiest to setup/implement:
Files - e.g. at the beginning of the execution read the value from file and when the value changes, write the new value to the file overwriting the old value
Key-Value store (e.g. Memcached or Redis) - You would need to install the memcached/redis server that would run as independent system process. Then you would connect to it and read the value and write the value when it changes. (Note these can be configured to be persistent or non-persistent storages, see next point or explanation)
APC cache - it's a special case of key-value store which works as a PHP extension rather than external service. The idea is still reading/writing the value stored in it. Note that this is not a persistent storage, meaning that when apache or php-fpm process is restarted the cache will be cleared and the value lost.
Database (e.g. MySQL or MongoDB). Again, you would have to install the database server and do some reading on how to use them.
There are certainly more options, but these are something to start with.
You need to use SQL to store that variable on server and AJAX requests to if you want to check the value in any set interval

Module scope in Node.js - Simultaneous Express requests

I'm using the id variable across different functions. The id is defined outside of the functions. Could this cause the variable to overwritten when receiving multiple simultaneous requests? Is the behavior the same when this is done in the browser instead of in Node.js?
var id;
server.get('/myapp', function (req, res, next) {
id = uuid.v4();
}
Yes, it causes conflicts
If your request handlers have any asynchronous operations in them which allow more than one request to be in flight at the same time, then YES, the use of a module level variable that more than one request may access will cause conflicts and one request could trounce the variable that another was using.
In a nutshell, don't do this
Module level variables are only for data that ALL requests can safely access and share. Data for a single request should be inside the request handler or a property on the request or response object or some similar-type object that is per-request and not shared among multiple requests.
Data needed only during the processing of a request
So, if you just need access to some data for the duration of this particular request, then you can put it in a local variable inside the request handler and you can pass it to any other functions you may call that need it while processing that request. Or, you can assign it as a property on the req object which is unique for each separate request and then pass the req object to any other functions you are calling that need access to the data.
Data needed from one request to the next, but different for each user
If you are trying to keep track of some data from one request to the next for one particular client, then you can't store it this way at all. You will likely need to use a session manager so you can create a lasting session that can be correlated with one particular browser from one request to the next. express-session is a popular session manager that makes it pretty easy.
The way a session manager works is that the first time a given browser hits your server, it is assigned a unique cookie. That cookie contains an encrypted session ID. Then, the session manager creates a session object that is indexed by that session ID. When a second request from the same browser comes in, then express-session gets that previously set cookie, decrypts the sessionID, looks up the corresponding session object and lets you then access any data you may have stored in the session object on behalf of this particular user.

How to store a json Object serverside and respond with it

I am trying to make a webpage and server where a user can enter in recipes and have them sent to (using JSON) the server and have the server store them in a file so that if the server is closed and reopened the user can request for something they've entered previously and can get it. I know how to send the JSON object both to the server and how to send the JSON object back to the client. I should note this can't use jquery.
What I need help with is how to store it in a file server side and get the contents from it later using a node.js server. They should all be stored in the same directory and I need to know how to get a list of the recipes in that directory. I've tried looking around but I can't seem to find the answer :(.
Example:
user makes a recipies
{ name:"cheese n waffles"
time:90,
ingredients:"cheese, eggs and waffles",
equipment:stove
};
Browser sends the JSON object to the server.
Client asks for a list of the recipes stored.
user asks for the recipe for spaghetti.
what I need help with:
server gets a list of the recipes it has stored
server takes the JSON object and stores it in /serverRootDir/recepiesStorage
server accesses /serverRootDir/recepiesStorage and gets the spaghetti recipe
You should be using a JSON based database such as MondoDB
It takes some learming however implementing with a text file will eventually become much more work and will function poorly
(Posted on behalf of the OP).
I have solved the problem. I will be using:
To read a file (access a recipe): fs.readFileSync() or fs.readFile().
To save a file (update a recipe): fs.writeFileSync() or fs.writeFile().
To get an array of files in a directory (retrieve the list of recipes): fs.readdirSync() or fs.readdir().

How to manage playerIds with SignalR and MVC

In game development with SignalR and MVC, I have to manage the player id's.
With MVC all Controllers is re created for each AJAX request so you can't store anything unless you use a database or store it in a cookie.
Does SignalR have any tools I can use for persisting something like playerIds for a game to ease it the situation?
You can't get the connection id's from SignalR, but you can register a ID per user on the OnConnected method. just override that function in your hub.
There might be a simpler way to do it, but one option is to pass the session key to JS, and from JS you can pass that session key (or any other unique identifier) back to the server on every command.
For example, while the user is first connected: OnConnected() -> gets the user unique id from JS and saves it locally to map each user to a unique id(possibly save that id as a group id so you can call send back msgs to that user according to the id).
After you that on each request sent to the hub you can attach the unique ID to it and then you can use it however you like.
I'm pretty sure you can find easier way to define a unique identifier and maybe a more suitable way to save your data in the hub.
Now that I think about it, you should also check this.

SpagoBI data set: use javascript to access to MongoDB data base

Since data set in SpagoBI could be created using scripts, I need to connect, query my MongoDB data base using javascript (or Groovy).
I need to use scripts to be able to execute aggregation on the mongoDB data, I can't use aggregation directly on my MongoDB because my data type is String
I dont know how to access my Database using scripts
Any ideas?
You should create a Mongo dataset. The steps to create are:
Step1: Create a Mongo datasource in the administrator console. Notes: the type must be JDBC and the value for Class input field must be "mongo"
JDBC: {unit_host}:{port}/${db}
CLASS: mongo
Step2: now you can create a dataset. The procedure is the same of the query datasets. The difference here is the language.. JS instead of SQL.
Take a look at the SpagoBI wiki in particolar here: http://wiki.spagobi.org/xwiki/bin/view/spagobi_server/data_set#HQueryDataSet28Mongo29
When connecting to mongoDB, you pass auth stuff in the url. Since the scripts lies on the client side, it would be hard to make the connection secure (unless you are talking about backend JavaScript). Anybody would be able to see how to connect to your DB and for instance delete all content.
I would suggest a simple api to interface the database. Then u control the access to what a user can do towards the database.
Or have I misunderstood the scenario?

Categories

Resources