Reputation: 23
Disclaimer: This is potentially a very nooby and redundant question, but I spent a day searching the web and couldn't find a single answer...
What I'm trying to do:
What I already set up: I set up the RethinkDB and installed deepstream on node.js with the rethinkdb storage connector and the rethinkdb search provider. Records created on the client side of my Deepstream get written into my RethinkDB correctly.
Server Side: index.js
#!/usr/bin/env nodejs
var DSServer = require("deepstream.io");
var DSRethinkConnector = require("deepstream.io-storage-rethinkdb");
// Setup the deepstream server
var server = new DSServer();
server.set("host", "0.0.0.0");
server.set("port", 6020);
// Setup the RethinkDB storage connector
server.set("storage", new DSRethinkConnector({
port: 28015,
host: "localhost",
splitChar: "/",
defaultTable: "chat"
}));
// Run the server
server.start();
Assumption A: I think this is only going to be possible using the rethinkdb search provider. Please correct me, if I'm wrong.
Assumption B: I looked at this site: https://deepstream.io/tutorials/integrations/db-rethinkdb/ and as I understand it, it continuously gives me real time search results from my RethinkDB table, where I could just say "find the latest 10 messages" . Please again, correct me if I'm wrong.
Assumption C: The tutorial in Assumption B tells me that I get the 10 latest messages in form of a list object. Now I read here: https://deepstream.io/docs/client-js/datasync-list/ that list objects emit events, every time a new entry is added. (entry-added event). Is this correct?
The Plan: I'd like to use this part of the example client side code from the previously mentioned tutorial to contiously retrieve the 10 latest messages:
var queryString = JSON.stringify({
table: 'book',
query: [
['title', 'match', '^Harry Potter.*'],
['price', 'lt', 15.30]
]
})
client.record.getList('search?' + queryString)
Problem A: I can't figure out what the proper queryString would have to look like to continuously keep a list of the latest 10 messages on the rethinkdb table?
Problem B: I'd then like to listen for the list's entry-added event to write the new entries/messages to the client's dom. But I can't find a single example/resource that explains how I would go about doing this?
This is my current client side code: index.html
<html>
<head>
<script src="deepstream.io-client-js/dist/deepstream.min.js"></script>
</head>
<body>
...
<script>
// Connect to the deepstream server
var ds = deepstream("192.168.192.201:6020").login();
// Create a unique name for the new record
var name = "messagethread__a_b/" + ds.getUid();
// Instantiate a new record
var record = ds.record.getRecord(name);
// Set several properties of the new record
record.set({
message: "Test 123",
from: "ClientA" //this says ClientB in index2.html
});
// Subscribe to changes on the table
var queryString = JSON.stringify({
table: 'messagethread__a_b',
query: [
['title', 'match', '^Harry Potter.*'],
['price', 'lt', 15.30]
]
})
record.getList('search?' + queryString);
</script>
</body>
</html>
Upvotes: 0
Views: 427
Reputation: 363
assumptions A, B and C are all correct. It's as easy as you described there's just a few things missing from your code snippets.
The rethinkdb search provider allows for "smart" queries which aren't available in deepstream itself just yet.
Adding a limit to the number of entries is simple, you'll just need to provide a couple of extra fields in your query.
var queryString = JSON.stringify({
table: 'book',
query: [
['title', 'match', '^Harry Potter.*'],
['price', 'lt', 15.30]
],
order: 'price',
desc: true,
limit: 10
})
client.record.getList('search?' + queryString)
Both "limit" and "order" are required by the rethinkdb sdk.
The call
client.record.getList
actually returns a deepstream List object[1], which you can then use to subscribe to different events.
All you'll need to do is:
const list = client.record.getList('search?' + queryString)
list.on('entry-added', (entryName, index) => {
// do stuff here
})
[1]https://deepstream.io/docs/client-js/datasync-list/
Upvotes: 0