Socket.io, Node.js and rethinkdb -
i using simple application in socket.io, node.js , rethinkdb. in server server.js file works 20 min. after 20 min code stops. my server .js :
var express = require('express'), path = require('path'), http = require('http'), io = require('socket.io'), r = require('rethinkdb'); var app = express(); app.configure(function() { app.set('port', process.env.port || 3000); app.use(express.logger('dev')); app.use(express.bodyparser()) app.use(express.static(path.join(__dirname, 'public'))); }); var server = http.createserver(app); io = io.listen(server); server.listen(app.get('port'), function() { console.log("express server listening on port " + app.get('port')); }); var count = 0; var = 0; io.sockets.on('connection', function(socket) { count++; socket.on('message', function(message) { url = message; //socket.join(url); r.connect({ host: 'localhost', port: 28015 }, function(err, conn) { if (err) throw err; r.db('test').table('online').insert({ socket_id: socket.id, news_id: message }).run(conn, function(err) { if (err) throw err; r.db('test').table("online").filter({ news_id: message }).count().run(conn, function(err, res) { if (err) throw err; = res; console.log("connect say" + say); // ip = socket.handshake.address.address; io.sockets.emit('pageview', { 'say': say, 'count': count, 'news_id': message }); }); }); }); }); socket.on('disconnect', function() { var news_id = 0; console.log("socket disconnected:" + socket.id); r.connect({ host: 'localhost', port: 28015 }, function(err, conn) { if (err) throw err; r.db('test').table("online").filter({ socket_id: socket.id })("news_id").run(conn).then(function(cursor) { cursor.each(function(err, item) { news_id = item; r.db('test').table("online").filter({ socket_id: socket.id }).delete().run(conn, function() { r.db('test').table("online").filter({ news_id: news_id }).count().run(conn, function(err, res) { if (err) throw err; = res; count--; console.log("disconnect say" + say); io.sockets.emit('pageview', { 'say': say, 'count': count, 'news_id': news_id }); }); }); //console.log("news_id"+news_id); }); }); }); }); });
my client.js:
var socket = io.connect("http://192.168.1.198:3000"); socket.on('connect', function () { console.log('socket connected'); socket.send(news_id); socket.on('pageview', function (msg) { if(msg.news_id == news_id) { //$('.count_icon').html(msg.say); console.log(msg.say); } }); });
while server.js running error:
unhandled rejection rqldrivererror: first argument `run` must open connection. @ new rqldrivererror (/root/node_modules/rethinkdb/errors.js:14:13) @ bracket.termbase.run (/root/node_modules/rethinkdb/ast.js:129:29) @ /home/node1/server.js:82:90 @ trycatcher (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/util.js:24:31) @ promise.erroradapter (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/nodeify.js:35:34) @ promise._settlepromiseat (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:528:21) @ promise._settlepromises (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/promise.js:646:14) @ async._drainqueue (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:177:16) @ async._drainqueues (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:187:10) @ async.drainqueues (/root/node_modules/rethinkdb/node_modules/bluebird/js/main/async.js:15:14) @ process._tickcallback (node.js:442:13)
the problem seems you're opening many connections , rethinkdb might closing of connections. add logic close connections every time open them, use 1 global connection or use rethinkdbdash handles using connection pool.
here's code using global connection (and using promises).
var express = require('express'), path = require('path'), http = require('http'), io = require('socket.io'), r = require('rethinkdb'); var app = express(); app.configure(function() { app.set('port', process.env.port || 3000); app.use(express.logger('dev')); app.use(express.bodyparser()) app.use(express.static(path.join(__dirname, 'public'))); }); var server = http.createserver(app); io = io.listen(server); server.listen(app.get('port'), function() { console.log("express server listening on port " + app.get('port')); }); var count = 0; var = 0; r.connect({ host: 'localhost', port: 28015 }) .then(function (conn) { io.sockets.on('connection', function(socket) { count++; socket.on('message', function(message) { url = message; //socket.join(url); promise.resolve() .then(function() { return r.db('test').table('online').insert({ socket_id: socket.id, news_id: message }).run(conn) }) .then(function() { return r.db('test').table("online").filter({ news_id: message }).count().run(conn); }) .then(function (res) { = res; console.log("connect say" + say); io.sockets.emit('pageview', { 'say': say, 'count': count, 'news_id': message }); }); }); socket.on('disconnect', function() { var news_id = 0; console.log("socket disconnected:" + socket.id); promise.resolve() .then(function () { return r.db('test').table("online").filter({ socket_id: socket.id })("news_id").run(conn); }) .then(function (cursor) { cursor.each(function(err, item) { news_id = item; promise.resolve() .then(function () { return r.db('test').table("online").filter({ socket_id: socket.id }).delete().run(conn); }) .then(function () { return r.db('test').table("online").filter({ news_id: news_id }).count().run(conn); }) .then(function (res) { = res; count--; console.log("disconnect say" + say); io.sockets.emit('pageview', { 'say': say, 'count': count, 'news_id': news_id }); }); }); }); }); }); })
Comments
Post a Comment