1

Is there some sort of Session-like variable to hold an array in Nodejs? What I meant is like something where I can define the name in other scope and be accessed in different scope (i.e: Variable("Array1") is defined in function A but accessed in function B and persists until it is destroyed).

The reason is I am using Meteor for slicing big files into small blobs and pass it back the chunk to the server. I tried to use the combination of fs.WriteFile and fs.AppendFile but somehow the file is mutilated along the way (the file is a video and playback error occurred with the copied file).

I read somewhere that blob can be rebuild by the constructor. However, I would need to pass this to a global or session-like variable in order to do so.

So...how can I use such thing in Nodejs?

2 Answers 2

3

There is such thing – it is called database :-)


When you're in Meteor, all files are loaded to a single running environment. Therefore, unlike in plain Node, a global variable created in one file can be accessed in any other one. So you can write

Slices = {};

in one file, and then in another say

Slices['Array1'] = ...

Notice there is no var keyword when defining the Slices object, otherwise it wouldn't be global but scoped to the file.


There is obviously one problem with the above method, and it's persistence over server reload. When the server crashes and restarts, or when you upload a new version, all such variables are recreated and you lose your data.

To prevent this, you need to store your variables in a place where they are retained permanently – a database of some kind. There are several solutions tailored for such runtime variables (such as Redis), but since you're using Meteor the natural solution would be to use the provided Mongo database. So just create a new collection on the server side

Slices = new Meteor.Collection('slices');

and use the usual find, insert, update and remove methods to access your variables.

Sign up to request clarification or add additional context in comments.

3 Comments

Good suggestion on the database usage for temporary/transit file chunks. But I found out that lack of blob on the nodejs makes my life difficult. +1
I'm not sure what do you mean by "lack of blob". Buffers and base-64 strings work pretty well, as well as byte strings if you're careful.
actually I tried to cut a large size files (around 1gb) and transfer it across the architecture to the server side. I used FileReader() readAsBinaryString (which returned as blob) but somehow when I concatenated it, something wrong happened to the file. I noticed that Nodejs write to as buffer, but I passed blob. This could be the problem maybe, but when I check Nodejs docs, I do not find any blobtype used in the framework. The best I could find is to transfer as ArrayByte but didnt work well with large chunk (browser crash for 1MB+ chunks). Well now I used collectionFS instead.
2

If everything happens in the same process space, you can use a module as a singleton.

Remember, even if a module is included multiple times, the same copy is returned.

So if you have this module:

module.exports = new Array();

And you include it by several other modules, each one of them will have the same array instance.

You can also have a more complex singleton:

var blocks = {};

module.exports.addBlock = function(name, block) {
    blocks[name] = block;
};

module.exports.getBlock = function(name) {
    return blocks[name];
};

module.exports.delBlock = function(name) {
    delete blocks[name];
};

module.exports.list = function() {
    return Object.keys(blocks);
};

In your different files, you would include and use this module like:

var blocks = require('./blocks');

console.log(blocks.list());

Read about module caching here.

1 Comment

Note: this is the right way when you're using plain Node.js, but won't work in a Meteor project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.