开发者

Node.js - Is this a good structure for frequently updated content?

开发者 https://www.devze.com 2023-03-14 00:48 出处:网络
As a follow-up to my question yesterday about Node.js and communicating with clients, I\'m trying to understand how the following would work.

As a follow-up to my question yesterday about Node.js and communicating with clients, I'm trying to understand how the following would work.


Case:

So, I have this website where content is updated very frequently. Let's assume this time, this content is a list of locations with temperatures. (yes, a weather service)

Now, every time a client checks for a certain location, he or she goes to a url like this: example.com/location/id where id corresponds to the id of the location in my database.


Implementation:

At the server, checktemps.js loops (every other second or so) through all the locations in my (mySQL) database and checks for the corresponding temperature. It then stores this data is an array within checktemps.js. Because temperatures can change all the time, it's important to keep checking for updates in the database.

When a request to example.com/location/id is made, checktemps.js looks into the array with a record with id = id. Then, it responds with the corresponding temperature.


Question:

Plain text, html or a开发者_运维问答n ajax call is not relevant at the moment. I'm just curious if I have this right? Node.js is a rather unusual thing to get a grasp on, so I try to figure out if this is logical?


At the server, checktemps.js loops (every other second or so) through all the locations in my (mySQL) database and checks for the corresponding temperature. It then stores this data is an array within checktemps.js

This is extremely inefficient. You should not be doing looping(every other second or so).

Modules

Below I would try and make a list of the modules(both node.js modules as other modules) I would use to do this efficient:

  • npm is a package manager for node. You can use it to install and publish your node programs. It manages dependencies and does other cool stuff.

    I hope sincerely that you already know about npm, if not i recommend you to learn about it as soon as possible. In the beginning you just need to learn how to install packages, but that is very easy. You just need to type npm install <package-name>. Later I would really like to advice you to learn to write your own packages to manage the dependencies for you.

  • Express is a High performance, high class web development for Node.js.

    This sinatra-style framework from TJ is really sweet and you should read the documentation/screencasts available to learn it's power.

    Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms.

  • Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

    Like Raynos said this extremely fast/sexy database has pubsub semantics, which are needed to do your question efficiently. I think you should really play with this database(tutorial) to appreciate it's raw power. Installing is easy as pie: make install

  • Node_redis is a complete Redis client for node.js. It supports all Redis commands, including MULTI, WATCH, and PUBLISH/SUBSCRIBE.

Prototype

I just remembered I helped another user out in the past with a question about pubsub. I think when you look at that answer you will have a better understanding how to do it correctly. The code has been posted a while back and should be updated(minor changes in express) to:

var     PORT        = 3000,
            HOST        = 'localhost',
            express = require('express'),
            io          = require('socket.io'),
            redis       = require('redis'),
            app         = module.exports = express.createServer(),
            socket  = null;

app.use(express.static(__dirname + '/public'));

if (!module.parent) {
    app.listen(PORT, HOST);
    console.log("Express server listening on port %d", app.address().port)
    socket = io.listen(app);

    socket.on('connection', function(client) {
        var subscribe = redis.createClient();
        subscribe.subscribe('pubsub'); // listen to messages from channel pubsub

        subscribe.on("message", function(channel, message) {
            client.send(message);
        });

        client.on('message', function(msg) {
        });

        client.on('disconnect', function() {
            subscribe.quit();
        });
    });
}

I have compressed the updated code with all the dependencies inside, but you while still need to start redis first.

Questions

I hope this gives you an idea how to do this.


With node.js you could do it even better. The request/response thingy is manifested in our heads since the beginning of the web. But you can do just one ajax request if you open the website/app and never end this call. Now node.js can send data whenever you have updates to the client. Search on youtube for Introduction to Node.js with Ryan Dahl (the creator of node.js), there he explains it. Then you have realtime updates without having to do requests by the client all the time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号