Reactive chat server in 3 lines, explained
It’s good to be wary about 3 line code, wow examples. But it’s hard not to want to show off the reactive aspects of DollarScript, so here goes:
Let’s break it down.
This simply sets the variable server to be the url of the server we’re going to create. The important parts are the hostname, socket and the type of event we’re interested in - in this case chatevent
.
Now we have the magic, the *=
is the subscriptive assignment operator. This is essential syntactic sugar to say subscribe to server and when a new value is available assign it to message.
Finally we have the reactive part. The first thing we do is create a ‘pair’, a pair is a map with a single key/value entry created using :
. The key is the type of event we are going to send, the value is the actual message itself. Because we have the variable message
in this expression any change to message
will cause the expression to be re-evaluated causing the pair to be published using the publish
operator to our previously defined server
.
And that my friends is it. It’s obviously a very simple example, however I feel it amply illustrates the value of the reactive side of Dollar.
This is a working example, you’ll also need the client code