Skip to content

Implementing a modified version of the Actor Model messaging system for use on the web.

License

Notifications You must be signed in to change notification settings

jint-method/actor-model-prototype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Actor Model Prototype

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.

References:

Overview

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.

About

Implementing a modified version of the Actor Model messaging system for use on the web.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published