Sonic Channel integration for Node. Used in pair with Sonic, the fast, lightweight and schema-less search backend.
Sonic Channel lets you manage your Sonic search index, from your NodeJS code. Query your index and get search results, push entries to your index and pop them programmatically.
🇫🇷 Crafted in Nantes, France.
Crisp |
👋 You use sonic-channel and you want to be listed there? Contact me.
Include sonic-channel
in your package.json
dependencies.
Alternatively, you can run npm install sonic-channel --save
.
node-sonic-channel
can be instanciated in search mode as such:
var SonicChannelSearch = require("sonic-channel").Search;
var sonicChannelSearch = new SonicChannelSearch({
host : "::1", // Or '127.0.0.1' if you are still using IPv4
port : 1491, // Default port is '1491'
auth : "SecretPassword" // Authentication password (if any)
}).connect({
connected : function() {
// Connected handler
console.info("Sonic Channel succeeded to connect to host (search).");
},
disconnected : function() {
// Disconnected handler
console.error("Sonic Channel is now disconnected (search).");
},
timeout : function() {
// Timeout handler
console.error("Sonic Channel connection timed out (search).");
},
retrying : function() {
// Retry handler
console.error("Trying to reconnect to Sonic Channel (search)...");
},
error : function(error) {
// Failure handler
console.error("Sonic Channel failed to connect to host (search).", error);
}
});
Use the same sonicChannelSearch
instance to query the search index:
// Notice: all methods return 'true' if executed immediately, 'false' if deferred (ie. TCP socket disconnected)
sonicChannelSearch.query("messages", "default", "valerian saliou", function(data, error) {
// Query results come in the 'data' argument
// Query errors come in the 'error' argument
});
If you need to teardown an ongoing connection to Sonic, use:
// Returns: true if proceeding close, false if already closed
sonicChannelSearch.close(function(data, error) {
// Handle close errors here
});
node-sonic-channel
can be instanciated in ingest mode as such:
var SonicChannelIngest = require("sonic-channel").Ingest;
var sonicChannelIngest = new SonicChannelIngest({
host : "::1", // Or '127.0.0.1' if you are still using IPv4
port : 1491, // Default port is '1491'
auth : "SecretPassword" // Authentication password (if any)
}).connect({
connected : function() {
// Connected handler
console.info("Sonic Channel succeeded to connect to host (ingest).");
},
disconnected : function() {
// Disconnected handler
console.error("Sonic Channel is now disconnected (ingest).");
},
timeout : function() {
// Timeout handler
console.error("Sonic Channel connection timed out (ingest).");
},
retrying : function() {
// Retry handler
console.error("Trying to reconnect to Sonic Channel (ingest)...");
},
error : function(error) {
// Failure handler
console.error("Sonic Channel failed to connect to host (ingest).", error);
}
});
Use the same sonicChannelIngest
instance to push text to the search index:
// Notice: all methods return 'true' if executed immediately, 'false' if deferred (ie. TCP socket disconnected)
sonicChannelIngest.push("messages", "default", "conversation:1", "I met Valerian Saliou yesterday. Great fun!", function(_, error) {
// Push errors come in the 'error' argument
});
If you need to teardown an ongoing connection to Sonic, use:
// Returns: true if proceeding close, false if already closed
sonicChannelIngest.close(function(data, error) {
// Handle close errors here
});
node-sonic-channel
can be instanciated in control mode as such:
var SonicChannelControl = require("sonic-channel").Control;
var sonicChannelControl = new SonicChannelControl({
host : "::1", // Or '127.0.0.1' if you are still using IPv4
port : 1491, // Default port is '1491'
auth : "SecretPassword" // Authentication password (if any)
}).connect({
connected : function() {
// Connected handler
console.info("Sonic Channel succeeded to connect to host (control).");
},
disconnected : function() {
// Disconnected handler
console.error("Sonic Channel is now disconnected (control).");
},
timeout : function() {
// Timeout handler
console.error("Sonic Channel connection timed out (control).");
},
retrying : function() {
// Retry handler
console.error("Trying to reconnect to Sonic Channel (control)...");
},
error : function(error) {
// Failure handler
console.error("Sonic Channel failed to connect to host (control).", error);
}
});
You may use the same sonicChannelControl
instance to administrate your Sonic server.
If you need to teardown an ongoing connection to Sonic, use:
// Returns: true if proceeding close, false if already closed
sonicChannelControl.close(function(data, error) {
// Handle close errors here
});
sonicChannelSearch.query(collection_id<string>, bucket_id<string>, terms_text<string>, done_cb<function>, [options{limit<number>, offset<number>}<object>]?)
➡️done_cb(results<object>, error<object>)
sonicChannelSearch.suggest(collection_id<string>, bucket_id<string>, word_text<string>, done_cb<function>, [options{limit<number>}<object>]?)
➡️done_cb(results<object>, error<object>)
sonicChannelIngest.push(collection_id<string>, bucket_id<string>, object_id<string>, text<string>, done_cb<function>)
➡️done_cb(_, error<object>)
sonicChannelIngest.pop(collection_id<string>, bucket_id<string>, object_id<string>, text<string>, done_cb<function>)
➡️done_cb(count<number>, error<object>)
sonicChannelIngest.count<number>(collection_id<string>, [bucket_id<string>]?, [object_id<string>]?, done_cb<function>)
➡️done_cb(count<number>, error<object>)
sonicChannelIngest.flushc(collection_id<string>, done_cb<function>)
➡️done_cb(count<number>, error<object>)
sonicChannelIngest.flushb(collection_id<string>, bucket_id<string>, done_cb<function>)
➡️done_cb(count<number>, error<object>)
sonicChannelIngest.flusho(collection_id<string>, bucket_id<string>, object_id<string>, done_cb<function>)
➡️done_cb(count<number>, error<object>)
sonicChannelControl.trigger(action<string>)
➡️done_cb(_, error<object>)
ℹ️ Wondering what Sonic is? Check out valeriansaliou/sonic.
node-sonic-channel
maintains persistent TCP connections to the Sonic network interfaces that are listening on your running Sonic instance. In case node-sonic-channel
gets disconnected from Sonic, it will retry to connect once the connection is established again.
You can configure the connection details of your Sonic instance when initializing node-sonic-channel
from your code; via the Sonic host and port.