README.md

# Wideact

Wideact is a inter-lingual actor system that bridges between languages. It consists of a server written in elixir, and several clients, written in different languages.

## Server

1. Add wideact to your list of dependencies in `mix.exs`:

        def deps do
          [{:wideact, "~> 0.0.1"}]
        end

2. Potentially change the port settings for the server in the `mix.exs` (running at 40444 by default).
3. Start it up with `mix run --no-halt`.

Once you start the server, you can just leave it there, and work on the clients.

## Protocol

The wideact protocol only carries messages over, not meaning. There are only two commands in the protocol:

        CONNECT <name>
        MESSAGE <name> <message>

One, obviously, connects us to the server; the other sends `message` to `user`.

> By adding message chaining, the syntax of <name> will change, so be aware of this if implementing your own client: when sending a message to the server, remove all blank spaces from them! 

## Clients

The clients given in this repo are barebone communication classes and should be extended for any sort of practical use.

### Java

For Java to connect to wideact, extend `wideact.Client` and add a listener if you want to receive messages.
```java
public class Reverser extends wideact.Client {
	public Reverser(String name) {
		super(name);

		this.addDataListener((sender, message) -> {
			System.out.println(sender + ": " + message);
			this.message(sender, new StringBuilder(message).reverse().toString());
		});
	}
}
```

Now we can create two instances of this class and ping-pong messages between them.
```java
final Reverser ping = new Reverser("ping");
final Reverser pong = new Reverser("pong");

ping.message("pong", "hello world!");

while(true) { Thread.yield(); }
```

### Ruby

Wideact was built to support your messages from whichever language they're coming from. Let's use the previous Java client setup to show both the Ruby client and this feature.

First, install the wideact gem:

```bash
gem install wideact
```

The next step is pretty straightforward:

```ruby
require 'wideact'
cli = Wideact::Client.new "ruby"
cli.add_listener do |sender, message|
  puts "#{sender}: #{message}"
  cli.message(sender, message.capitalize!)
end

cli.message("ping", "hello from ruby!")
loop do; end
```

You can see that we're sending a message to ping, in Java. Don't worry if you don't have it running: the sent messages will wait in ping's inbox until it reconnects.

### Javascript

To run wideact with client-side, browser javascript, either use the server given in this repo or make your own from it: the given one uses Ruby (Sinatra) as a basis and builds on top. The only thing the server-side client is doing is passing a message from the TCP server running from the Ruby client to the websocket server opened in the web stack. The client-side library is included in the `public/` directory.

A script that showcases the client in javascript could be written like this (and run by starting the `ruby server.rb` and going to the browser, to `localhost:4567`):

```javascript
(function() {
        w = new Wideact.Client("confuser");

        w.add_listener(function(name, message) {
                console.log(name + ": " + message);
                var shuffled = message.split('').sort(function() {
                        return 0.5 - Math.random()
                }).join('');

                w.message(name, shuffled);
        });

        w.message("pong", "please, invert this.");
})();
```

This will send a message to `pong`, which will then respond to our javascript client `confuser` (it shuffles words), which will shuffle the inverted response and send it back. Hilarity ensues.

## Features

Wideact is a sturdy message-passing service, with server-instance persistence and without hard-drive persistence outside of that. This means that when you send a message, it will wait for the client it's sent to to arrive, until you turn the Wideact server off. After reboot, all mailboxes are empty.

You may notice that all of the examples here are servers, in a way: for every message they receive, they also send something back, usually doing a transformation on the input.

This isn't a neccessity: a listener can run some code that doesn't respond to the original sender, and this will be the case for many of the actors (worker factories, workers, logs, etc.)

> Cool incoming feature: `cli.message("pong > confuser", "invert, then shuffle.")` will be the notation for message chaining; this feature will  make data serving actors into full-on transformers and allow greater clarity (and maybe standardized services?).

## Like it?

I'm not a good elixir programmer. I wrote the server following tutorials and remembering erlang from a few months of working with it some years back. If you find any issues or gaping holes or optimizations that could be made, please make yourself at home by contacting me or fixing it yourself!

Writing clients for other languages is also a great plus. Please make a pull request if you do and I will gladly include it.

Last but not least, use it! I think that wideact is a thing that can be useful -- that is how and why I started doing it, so nothing would make me happier than to see people using it in practice!