See the docs for the update
command:
npm update [-g] [<pkg>...]
This command will update all the packages listed to the latest version (specified by the tag config), respecting semver.
Additionally, see the documentation on Node.js and NPM installation and Upgrading NPM.
The following original answer is from the old FAQ that no longer exists, but should work for Linux and Mac:
How do I update npm?
npm install -g npm
Please note that this command will remove your current version of npm. Make sure to use sudo npm install -g npm
if on a Mac.
You can also update all outdated local packages by doing npm update
without any arguments, or global packages by doing npm update -g
.
Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already. (Consider, if there is ever a bug in the update command.) In those cases, you can do this:
curl https://www.npmjs.com/install.sh | sh
To update Node.js itself, I recommend you use nvm, the Node Version Manager.
Option 3
When you update MySQL from PHP you publish those changes to node.js via redis publish
command(publish from PHP when mutating database). From node.js I would receive those changes in real-time thanks to Redis's subscribe. Then I would just broadcast them to users interested via socket.io. You could for example publish
to channel mysql
. Take for example the following SQL statement => INSERT INTO comments (1, "Hello World")
. Where 1
is something like userid, and Hello World
would be something like the comment. I probably would not publish SQL-statement to that channel, but JSON instead which I can easily use both from JavaScript(JSON.stringify / JSON.parse) and PHP(json_encode / json_decode).
Update
You don't run a cron-job because this would defeat the purpose off Redis's pubsub. Take for example I visit your website which is a blog at http://localhosts
. I read an article at http://localhost.com/a.php
. Below on the site you provide a form which I can use to post a comment to that article:
a.php
<html>
<head>
<title>Interesting blog post</title>
</head>
<body>
<div id="article">This is interesting</div>
<div id="comments">
<div class="comment">
<div class="from">Alfred Said at 22:34</div>
<div class="message">Hello World</div>
</div>
</div>
<form action="post.php" method="post">
<label for="name">Your name</label><br />
<input type="name" id="name" name="name" /><br />
<label for="message">Your Message:</label><br />
<textarea id="message" name="message"></textarea>
<input type="submit" />
</form>
<script src='jquery.min.js'></script>
<script src='http://localhost:8888/socket.io/socket.io.js'></script>
<script type="text/javascript">
$(document).ready(function () {
var socket = io.connect('http://localhost:8888');
socket.on('message', function (json) {
var obj = $.parseJSON(json);
alert('in here: ' + obj.name);
});
});
</script>
</body>
</html>
I submit the form which has action attribute http://localhost/postcomment.php
. But this is the important part! At post.php
you retrieve the data I posted and insert it into MySQL using INSERT INTO comments (1, "Hello World")
. When this mutation happens you also need to inform node.js process which is continually listening to channel mysql
:
post.php:
<?php
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
require("./Predis.php");
$redis = new Predis\Client();
$obj = array(
'name' => $_POST['name'],
'message' => $_POST['message']
);
$json = json_encode($obj);
$redis->publish("mysql", $json);
echo $json;
post.php requires predis.
The node code with node_redis would look something like:
var redis = require('redis'),
subscriber = redis.createClient(),
express = require('express'),
store = new express.session.MemoryStore(),
app = express.createServer(
express.bodyParser(),
express.static(__dirname + '/public'),
express.cookieParser(),
express.session({ secret: 'htuayreve', store: store}))
sio = require('socket.io');
app.listen(8888, '127.0.0.1', function () {
var addr = app.address();
console.log('app listening on http://' + addr.address + ':' + addr.port);
});
var io = sio.listen(app);
io.configure(function () {
io.set('log level', 1); // reduce logging
});
io.sockets.on('connection', function (socket) {
socket.join('mysql');
socket.on('disconnect', function () {
});
});
subscriber.on('message', function (channel, json) {
// this will always retrieve messages posted to mysql
io.sockets.in('mysql').json.send(json);
});
subscriber.subscribe('mysql');
This samples depends on the following packages, which you can install via npm
npm install socket.io
npm install redis
npm install express
Always when I post the form post.php
, I also publish these changes to redis. This part is important! The node.js process is always receiving those changes thanks to Redis's pubsub. Every time when a php script mutates the database you should publish these changes to Redis with publish
.
P.S: Hope this is clear. Maybe later when I have some time available I update with maybe little snippet...
Best Answer
Yes, you have to create a new redis client for every io request. It is heavy and not scalable. But creating a new redis client connection does not consume much memory. So if your system user count is not more than 5000 then it is ok. To scale you can add in slave redis server to resolve the heavy publish and subscribe and if you are concerned about creating a lot of connections then you can increase your OS uLIMIT.
You don't need to store socket.io client in message sent. Once redis received subscribed channel message. It will send message to particular io client.
To subscribe multi channel. I suggest you to pre-store all user with more than one channel(You can use storage Mongodb or redis).