kriskowal/q-connection — 246★ on GitHub (JavaScript). A JavaScript library for communicating asynchronously with remote objects using promises.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
This library makes it possible for objects to communicate
asynchronously between memory-isolated JavaScript contexts,
including pipelining interactions with results. Promises
serve as proxies for remote objects.
Q-Connection works in Node and other CommonJS module loaders like
Browserify, Mr, and Montage.
This is how it looks:
var Q = require("q");
var Connection = require("q-connection");
var remote = Connection(port, local);
The remote object is a promise for the local object
on the other side of the connection. Likewise, the other
side of the connection will get a promise for your local
object. You are not obliged to provide a local object,
depending on which end of the connection is providing a
service.
If the remote or local object is not serializable,
like functions or objects with methods, the other side will
receive a promise but you will have to “send messages” to
the promise instead of interacting directly with the remote
object. When you invoke a method on a remote object, you
get a promise for the result and you can immediately
pipeline a method call on the result. This is the secret
sauce.
The port is any W3C message port, web worker, or web
socket. In the W3C’s infinite wisdom, these do not have a
unified API, but Q-Connection will normalize them internally.
// To communicate with objects in a worker
var worker = new Worker("worker.js");
var child = Connection(worker, local);
// Inside a worker, to communicate with the parent
var parent = Connection(this);
// To communicate with a remote object on the other side of
// a web socket
var socket = new WebSocket("ws://example.com");
var remote = Connection(socket, local);
// To communicate with a single frame on the same origin
// (multiple frames will require some handshaking event sources)
var iframe = document.frames[0];
var child = Connection(iframe.contentWindow, local, {
origin: window.location.origin
})
// To communicate with a parent frame on the same origin
var child = Connection(window, local, {
origin: window.location.origin
})
// With a message port
var port = new MessagePort();
var near = Connection(port[0]);
var far = Connection(port[1]);
Your local value can be any JavaScript value, but it is
most handy for it to be an object that supports an API and
cannot be serialized with JSON.
var Q = require("q");
var counter = 0;
var local = {
"next": function () {
return counter++;
}
};
In this case, the local object has a "next" function that
returns incremental values. Since the function closes on
local state (the counter), it can't be sent to another
process.
On the other side of the connection, we can asynchronously
call the remote method and receive a promise for the result.
The connection is bi-directional. Although you do not need
to provide and use both local and remote values on
both sides of a connection, they are available.
You can asynchronously interact with any value using the Q
API. This chart shows the analogous operations for
interacting with objects synchronously and asynchronously.
All of the asynchronous functions return promises for the
eventual result. For the asynchronous functions, the value
may be any value including local values, local promises, and
remote promises.
The benefit to using the asynchronous API when interacting
with remote objects is that you can send chains of messages
to the promises that the connection makes. That is, you can
call the method of a promise that has not yet been resolved,
so that message can be immediately sent over the wire to the
remote object. This reduces the latency of interaction with
remote objects by removing network round-trips.
A chain of dependent operations can be contracted from:
Where the dotted lines represent messages traveling through
the network horizontally, and through time vertically.
Ports
Q-Connection handles a variety of message ports or channel types. They are
all internally converted into a Q Channel. If you are using a message
channel that provides a different API than this or a WebWorker,
WebSocket, or MessagePort, you can adapt it to any of these interfaces
and Q-Connection will handle it.
This is probably the simplest way to create a channel duck-type,
assuming that you’ve got a connection instance of the Node variety.
var port = {
postMessage: function (message) {
connection.send(message);
},
onmessage: null // gets filled in by Q-Connection
};
connection.on("message", function (data) {
port.onmessage({data: ""})
});
var remote = Connection(port, local);
Here's an example showing adapting socket.io to the message port.
var port = {
postMessage: function (message) {
socket.emit("message", message);
},
onmessage: null // gets filled in by Q-Connection
};
socket.on("message", function(data) {
port.onmessage({data: data});
});
var remote = Connection(port, local);
Q Channels
get() returns a promise for the next message from the other
side of the connection. get may be called any number of times
independent of when messages are actually received and each call
will get a promise for the next message in sequence.
put(message) sends a message to the remote side of the
connection.
close(reason_opt) indicates that no further messages will be
sent.
closed a promise that is fulfilled with the reason for closing.
Q-Connection exports an indefinite Queue that supports this API which
greatly simplifies the implementation of adapters.
get() returns a promise for the next value in order that is
put on the queue. get may be called any number of times,
regardless of whether the corresponding value is put on the queue
before or after the get call.
put(value) puts a message on the queue. Any number of
messages can be put on the queue, indepent of whether and when the
corresponding get is called.
close(reason_opt) indicates that no further messages will be
put on the queue and that any promises for such messages must be
rejected with the given reason.
closed a promise that is fulfilled when and if the queue has
been closed.
Web Workers and Message Ports
Q-Connection detects ports by their postMessage function.
postMessage(message)
onmessage(handler(message))
Web Sockets
Q-Connection detects Web Sockets by their send function. It takes the
liberty to start the socket and listens for when it opens.
send(message)
addEventListener(event, handler(event))
start()
open event
close event
Memory
Q-Connection uses an LRU cache of specified size. The default size is
infinite, which is horribly leaky. Promises between peers will stick
around indefinitely. This can be trimmed to something reasonable with
the max option.
var remote = Connection(port, local, {max: 1024});
The least frequently used promises will be collected. If the remote
attempts to communicate with a collected promise, the request will be
ignored. The minimum working set will vary depending on the load on your
service.
To be notified when communication is attempted with a collected promise
set the onmessagelost option.
var remote = Connection(port, local, {
max: 1024,
onmessagelost: function (message) {
console.log("Message to unknown promise", message);
}
});
Does kriskowal/q-connection have a project website?
No homepage URL was recorded for kriskowal/q-connection in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How many stars does kriskowal/q-connection have?
kriskowal/q-connection has 246 GitHub stars — refresh the page for the live number, or check github.com/kriskowal/q-connection. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is kriskowal/q-connection open source?
Yes — kriskowal/q-connection ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/kriskowal/q-connection.
What is kriskowal/q-connection?
kriskowal/q-connection (kriskowal/q-connection) is a JavaScript project on GitHub. From the project's own README: A JavaScript library for communicating asynchronously with remote objects using promises.
Where do I read more about kriskowal/q-connection?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/kriskowal/q-connection is the definitive source.
Read full README in the tab above.
Want a second opinion on q-connection?
Ask an AI that can read this page — one click and you get its take on q-connection.