I am developing a library that unusually has a significant client part. In summary GenBrowser aims to give clients/browser an identifier that can be used to send messages to/from/between clients. In essence a pid.
For example
# browser 1
const { address, mailbox, send } = await GenBrowser.start('http://gen_browser.dev')
console.log(address)
# server
iex> {:ok, client} = GenBrowser.decode_address(address)
iex> GenBrowser.send(client, %{text: "From the server"})
# browser 2
send(address, {text: 'From browser 2'})
# browser 1
var message1 = await mailbox.receive({timeout: 2000})
message1.text
# From the server
var message2 = await mailbox.receive({timeout: 2000})
message2.text
# From browser 2
The security model relies on signing addresses that are sent out of the server, that is why the signed address needs decoding on the server.
I am not very up to date on the front end world and so want some advice on how to proceed with this project.
Ideally I want to keep the whole thing in one project and use as much of the Elixir ecosystem as possible. However that might be limiting.
Advice on the JavaScript API
There are two options for working with messages received.
-
mailbox.receive()
that takes an optional timeout an returns a promise that completes on the next message -
mailbox.setHandler(messageCallback, closeCallback)
The first callback is called whenever a message is received, the second when the mailbox has been closed permanently.
These names api’s come from their erlang world equivalent receive
and handle_*
. They look reasonably sensible in the JavaScript world but could probably be more idiomatic
Npm publishing
The project has a JavaScript build step and the code is always to be used in a client.
- Would you expect this to be available on npm as well as a CDN
- If so should only the source (or only the bundle) be published to npm
JS Documentation
ExDoc has spoiled me for ease of setting up documentation. In these cases I am probably just looking for the most standard/simple way of doing things
- What is the recommended way to document a JavaScript library?
- Is there a way to integrate this documentation into the hex documentation?
Redux as Actors
With my (limited) knowledge of redux I think that redux and GenServers look quite similar.
Hence the name of this project.
Do you think this is a helpful analogy when describing processes to JavaScript developers.
Within a single process or store there is a single state tree.
I often say that sending a message is like dispatching on a remote store.
Is there any better way of explaining things? Should I just say actor model and not confuse the issue with mentioning redux