Giter Site home page Giter Site logo

rethinkdb-websocket-client's Introduction

npm version Travis

rethinkdb-websocket-client

RethinkDB JavaScript driver monkey-patched to connect via WebSocket. Works in browser, Electron and Node.JS environments.

What is this?

This library wraps the official JavaScript RethinkDB driver, monkey-patching the node.js net module so that it connects over WebSocket. Other than calling RethinkdbWebsocketClient.connect instead of rethinkdb.connect, the API is identical. And other than the HTTP upgrade request at the start, the protocol over the wire is unchanged.

Since RethinkDB does not accept WebSocket connections, you will have to use a proxy on the server that accepts WebSocket connects and proxies them to the RethinkDB TCP port:

  • websockify is a server that listens for incoming WebSocket connections, and blindly forwards traffic in both directions to a specified TCP address. To set up a websockify server at that forwards WebSocket port 8015 to RethinkDB running locally on port 28015, run ./run localhost:8015 localhost:28015
  • rethinkdb-websocket-server is a node.js server library that functions similarly to websockify. However, it parses incoming RethinkDB queries from browser clients and runs them through custom validation before forwarding the query to the RethinkDB server.

How do I use this?

This package should be installed with npm. You probably want to use something like webpack or browserify to include it in your web application. In theory it will also run on node.js, but I have not yet tested that.

Here is a simple example of how to use it:

var RethinkdbWebsocketClient = require('rethinkdb-websocket-client');
var r = RethinkdbWebsocketClient.rethinkdb;

// In case you want bluebird, which is bundled with the rethinkdb driver
var Promise = RethinkdbWebsocketClient.Promise;

var options = {
  host: 'localhost',       // hostname of the websocket server
  port: 8015,              // port number of the websocket server
  path: '/',               // HTTP path to websocket route
  wsProtocols: ['binary'], // sub-protocols for websocket, required for websockify
  wsBinary: 'arraybuffer',  // specify which binary type should be used for WS (optional)
  secure: false,           // set true to use secure TLS websockets
  db: 'test',              // default database, passed to rethinkdb.connect
  simulatedLatencyMs: 100, // wait 100ms before sending each message (optional)
};

RethinkdbWebsocketClient.connect(options).then(function(conn) {
  var query = r.table('turtles');
  query.run(conn, function(err, cursor) {
    cursor.toArray(function(err, results) {
      console.log(results);
    });
  });
});

Other environments

React

For React integration, see the react-rethinkdb library.

Node.JS and Electron

To use inside Electron or on the server in Node.JS (as opposed to the browser), use the following path when importing the module:

var RethinkdbWebsocketClient = require('rethinkdb-websocket-client/dist/node');

If you need to route the WebSocket through a proxy server you can provide your own agent via the wsProtocols configuration parameter, e.g.:

var Socks = require('socks');
var options = {
  host: 'localhost',       // hostname of the websocket server
  port: 8015,              // port number of the websocket server
  path: '/',               // HTTP path to websocket route
  wsProtocols: {
    agent: new Socks.Agent({
      proxy: { ipaddress: '127.0.0.1', port: 8080, type: 5 }
    })
  },
  secure: false,           // set true to use secure TLS websockets
  db: 'test',              // default database, passed to rethinkdb.connect
  simulatedLatencyMs: 100, // wait 100ms before sending each message (optional)
};

rethinkdb-websocket-client's People

Contributors

babakness avatar dependabot[bot] avatar gutenye avatar mikemintz avatar niieani avatar shareefalis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rethinkdb-websocket-client's Issues

Out-of-order messages on react-native when using simulatedLatencyMs

When I implemented simulatedLatencyMs using setTimeout, I assumed that multiple calls to setTimeout with the same interval would have the callbacks executed in the same order that the timeouts were created.

After having some message-ordering issues with react-native that went away when I disabled simulatedLatencyMs, I think it's not safe to rely on. Some quick googling indicates I shouldn't rely on it: http://stackoverflow.com/q/11771558/515380

It might make sense just to remove this functionality and let developers use their own tools to simulate latency when developing locally. Unfortunately, chrome dev tools doesn't support websocket yet for throttling.

getting wrong data on frontend

Using rethinkdb-websocket-client and rethinkdb-websocket-server with whitelist query feature is fetching me wrong data on the frontend.

The query I am executing is simply

r.table('tableName');

its getting allowed via the server

[ALLOW] r.table("tableName").opt("db", r.db("dbName"))

if I run the query on the admin console of rethinkdb I get 8 objects where 4 have status : "placed" and 4 have status : "executed" but on the frontend I am getting all 8 status : "executed"

Yes its very strange.

"Uncaught TypeError: Illegal invocation" when using changes()

Hi,
I'm not quite sure which component caused this issue, but I suspect it's something websocket-related.

RethinkDB + Websockify is hosted in Tutum with the following Stackfile (mostly docker-compose.yml):

rethinkdb:
  image: anapsix/rethinkdb:latest
  ports:
    - "8080:8080"
    - "28015:28015"
websockify:
  image: siboulet/websockify:latest
  command: '8015 rethinkdb:28015'
  ports:
    - "8015:8015"

When running the example below, I get "Uncaught TypeError: Illegal invocation"

  var options = {
    host: 'fbae7f61-dockhero.node.tutum.io',       // hostname of the websocket server
    port: 8015,              // port number of the websocket server
    path: '/',               // HTTP path to websocket route
    wsProtocols: ['binary'], // sub-protocols for websocket, required for websockify
    secure: false,           // set true to use secure TLS websockets
    db: 'domaino',              // default database, passed to rethinkdb.connect
    simulatedLatencyMs: 100, // wait 100ms before sending each message (optional)
  };

  RethinkdbWebsocketClient.connect(options).then(function (conn) {
    r.table('domains').get("2ezrJu4iqoo4h2").changes().run(conn, function(err, cursor) {
      cursor.each(console.log);
    });
  });

Just to give some context, queries which just fetch data once (like r.table('domains').run(...)) work fine. Queries which wait for the changes in a collection would fail when the change actually happens. Queries waiting for the changes in a point (like in the code above) fail immediately.

I'll keep the server mentioned in the code above alive for the next several days so that you could reproduce the issue by running the code snippet above as is.

Tested in Chrome Version 46.0.2490.86 (64-bit) on Mac OS and latest Safari.
Thanks, Vladimir

Port 80

For a while I have been trying to figure out how to proxy through nginx on port 80 for the websockets port. Some networks don't allow a webpage to use ports other than 80/443. I have tried a bunch of times to get this working. I have the rethinkdb-websocket-server running behind nginx. Does anyone have any insight?

Reconnecting

Hi,

I'd like to ask, how can I solve reconnecting issue using this library. Or should I do connect every time I want to make a call? That doesn't really make a sense when websockets are involved. When a server goes down, I'd expect a websocket to try to reconnect, or am I wrong?

Publish webpack directory / plugins

Webpack plugins should be published in the NPM repository for custom scenarios. In my case, I want to use rethinkdbdash as the client instead of the standard one combined with your TcpPolyfillPlugin and TlsStubPlugin. I was able to copy& paste those plugins and configure Webpack with a custom target to load them for rethinkdbdash. If the plugins were published it would have been an easier process :)

webpack issue "import and export may only appear at the top level"

npm run prepublish doesn't produce the same index.js in dist that one gets from npm install rethinkdb-webdocket-client. The version of dist/index.js via npm install is in es5, the one produced by the local webpack config is in es6 with stated issues. No biggie but seems like an update to scripts in package.json makes sense.

Support for browsers that don't support websockets

I was browsing your code and noticed that you don't have any support for browsers that don't support websockets. So my assumption would be that this line would fail since there wouldn't be WebSocket() in the global namespace.

Are you planning to support for browsers that don't support websockets? Maybe switching to something similar to Socket.IO would be a good alternative. This would also solve your reconnecting problem (#7).

Question about Secure Feature

What is the proper way to use the secure feature with the corresponding rethinkdb-websocket server? I have a rethinkdb server running on AWS. Created my SSL keys. The ws server seems to connect fine, but my browser clients can no longer connect.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.