The purpose of this prototype is to explore the idea of using a modified version of the Actor Model messaging system for the web. This prototype was inspired by the prototypes created by Surma and Paul Lewis for their presentations at Chrome Dev Summit 2018 & 2019.
This is NOT a library. The source code available within the repo is UNTESTED and IS NOT recommended for production use without proper testing. This is a proof of concept. Checkout the live demo at https://messaging.jintmethod.dev.
- The Actor Model: brief introduction video explaining the Actor Model
- An Actor, a model and an architect walk onto the web...: introduction to the idea of using the Actor Model on the web
- The 9am rush hour: introduction to the idea of utilizing workers for non-DOM related work
- Lights, Camera, Action!: expanding upon the Actor Model on the web article
- Architecting Web Apps: video showcasing the use of the Actor Model in a web application
- The main thread is overworked & underpaid: a video expanding upon the idea that the main thread should be reserved for UI work
All scripts communicate using the Broadcaster
class.
The class is available using:
import { broadcaster } from './broadcaster.js';
When a script wants to receive a message it registers an inbox with the Broadcaster:
broadcaster.hookup('my-inbox-name', this.inbox.bind(this));
Anything can send a message through the Broadcaster:
broadcaster.message('recipient-inbox-name', { type: 'message-type' });
The broadcaster.message()
method requires a recipient name along with a MessageData
object that contains a message type. Additional values can be sent within the MessageData
object as long as they're a transferable structure.
Inboxes DO NOT have unique name. Several inboxes can be registered under the same name. When a message is sent to an inbox foo
all inboxes labeled as foo
will receive the message.
Scripts can register several inboxes. All inboxes can point to one inbox callback function:
import { broadcaster } from './broadcaster.js';
class FooClass
{
private inbox(data:MessageData) : void
{
console.log(data);
}
private init()
{
broadcaster.hookup('foo', this.inbox.bind(this));
broadcaster.hookup('bar', this.inbox.bind(this));
}
}
The broadcaster.hookup()
function returns a unique ID for the registered inbox:
const inboxId = broadcaster.hookup('foo', this.inbox.bind(this));
An inbox can be disconnected from the Broadcaster:
broadcaster.disconnect(inboxUniqueId);
By default broadcaster.message()
sends messages using the UDP
message protocol and the message will be dropped if an inbox is not found for the recipient.
A TCP
message protocol is available. Messages will be resent until a recipient inbox is found or the maximum number of attempts has been reached:
broadcaster.message('foo', { type: 'test' }, 'TCP');
The maximum number of attempts before a TCP
message is dropped defaults to 100
but the value can be overridden:
broadcaster.message('foo', { type: 'test' }, 'TCP', 200);
If a message should never be dropped the Infinity
value can be used instead of an integer.
On low-end devices (<= 4gb RAM) disconnected inboxes are removed every minute.
On all other devices disconnected inboxes are removed every 5 minutes.