Coder Social home page Coder Social logo

munshkr / flok Goto Github PK

View Code? Open in Web Editor NEW
234.0 8.0 33.0 5.07 MB

Web-based P2P collaborative editor for live coding sounds and images

Home Page: https://flok.cc

License: GNU General Public License v3.0

JavaScript 10.30% CSS 0.44% TypeScript 88.59% HTML 0.68%
collaborative-editing codemirror live-coding livecoding flok foxdot hydra tidalcycles supercollider webrtc

flok's Introduction

Flok

Web-based P2P collaborative editor for live coding music and graphics

Features

  • Similar to Etherpad, but focused on code evaluation for livecoding.
  • Multiple separate slots for different languages and tools.
  • REPL plugins: allows user to locally evaluate code from interpreters (like Haskell, Ruby, Python, etc.):
  • Web Plugins, for languages embedded in editor:

Usage

Public server

WARNING - Please Read: Using a public server can be dangerous as anyone can execute code on your computer via Flok, so please make sure you only share your session URL to trusted users and friends when you use a public server. I will not be held responsible for any damaged caused by Flok. You have been warned.

This is a list of known public servers:

Create a session

When you enter a Flok server, you will be shown an empty session with a single slot, with a target selected (usually hydra). You can either change the target by clicking on the target selector at the top-left corner of the slot, or add more slots by clicking on the Command button (at the top-right corner of the screen), and then clicking on Add Pane, or Configure.

A target is the language or tool that Flok will communicate to create sound or images within the web page, or through flok-repl.

If you clicked on Configure, enter the name of the targets, separated with commas. You can use a target multiple times and Flok will create that many number of slots to write code. Currently the maximum number of slots is 8.

Examples:

  • tidal, foxdot, hydra: 3 slots, with tidal, foxdot and hydra respectively.
  • sclang, sclang, sclang, hydra, hydra: 5 slots total, the first 3 with sclang and the last 2 with hydra.
  • mercury, hydra: 2 slots total, one with Mercury and one with Hydra.

You will also be asked to enter a nickname. This is the name that will be shown to other users under your cursor, when you write code. You can change it any time by clicking on the Change Username inside the Command menu.

Now, just copy the URL and share it with your friends! They will be able to join the session and write code with you :-)

If you are using any target that requires a REPL, you will need to start it separately. See the Connect REPLs to Flok section below.

Connect REPLs to Flok

The last step is to start flok-repl, to connect Flok with your REPLs.

Just click on the REPLs button at the top-right corner of the screen, and copy the command shown there. It will look something like this:

npx flok-repl@latest -H wss://next.flok.cc \
  -s mammoth-tan-roundworm-17a5d501 \
  -t tidal \
  -T user:munshkr

This command will automatically try to download and install flok-repl and start it, connecting it to your session. If you have multiple different targets with REPLs, the command will start one process for each target from the same command.

Local server

In case you want to use Flok without Internet connection and/or you don't want to play Flok on a public server, you can easily start a local Flok server.

To start the server, simply run:

npx flok-web@latest

You can also install both web and repl packages beforehand (e.g. if you already know you won't have internet access on the venue) with:

npm install -g flok-web@latest flok-repl@latest

This will download and install the latest Flok web version and start a server.

Your local server will be available on http://localhost:3000 from your computer. To share the URL with your friends, change localhost with your local LAN IP. flok-web will try to guess your local IP in your LAN, and show it on the console, but it might not always work.

Secure mode (https)

In some cases, it's needed to run Flok in secure mode, using https. This is needed for some browsers, like Chrome, to allow access to the microphone and camera (which might be needed for some targets, like Hydra). You can easily run Flok in secure mode by passing the --secure parameter:

npx flok-web@latest --secure

Note about remote users (not LAN)

Sharing your local server to other users in the Internet is a bit more complicated, and it depends on your router and network configuration. You will need to configure your router to forward the port 3000 to your computer, and then share your public IP with your friends. You can find your public IP by visiting https://whatismyipaddress.com/. Also make sure to check your firewall settings, to allow incoming connections to port 3000. It's possible that some of your remote friends won't be able to connect to your local server, because of their own network configuration.

Supported REPL targets

TidalCycles

Use flok-repl with the -t tidal parameter.

You can specify custom options with the --extra parameter, by passing a JSON object, like this:

--extra '{ "bootScript": "/path/to/my/boot.hs", "useStack": true }'

Extra options
  • bootScript: Path to a custom initialization script.

  • useStack: Uses stack exec -- ghci instead of plain ghci. Use this if you installed Tidal using Stack.

  • ghci: Use a specific Ghci command instead of plain ghci. This overrides useStack option, if used too.

Sardine

Use flok-repl with the -t sardine parameter. In order to make it work, the sardine REPL must be included to your PATH. It should already be the case if you followed a regular install.

Extra options
  • python: Path to your custom sardine Python REPL. Use this if you need to target a specific install of Sardine (Python version, different path, etc).

FoxDot

Use flok-repl with the -t foxdot parameter.

Extra options
  • python: Path to Python binary. Use this if you need to use a custom Python version.

SuperCollider

In the case of SuperCollider, there are two types of REPLs: sclang and remote_sclang. The first one tries to run a sclang process and interact with it, while the second one uses FlokQuark to communicate with SC. Read more for installing and using it.

sclang vs. remote_sclang
  • As of today sclang does not currently work on Windows, you will have to use remote_sclang.

  • remote_sclang needs SC IDE to be running, and you need FlokQuark installed and running there.

  • If you use remote_sclang, you won't see Post messages from Flok, because FlokQuark does not currently capture Post messages and errors. It is recommended to deattach the Post window and have it visible while using Flok.

  • sclang can't use any GUI object (like Scopes, Proxy mixers, etc.). You will need to use remote_sclang + SC IDE for this.

Hydra

Hydra is a video synth and coding environment, inspired in analog video synthesis, that runs directly in the browser and is already included in the web App. You don't need to install anything as it runs on the browser. Just use the hydra target to execute Hydra code.

You can also use p5.js within a hydra target, like you would in the official Hydra editor.

Mercury

Mercury is a minimal and human readable language for livecoding of algorithmic electronic music. Below is a link to steps for connecting Flok to either the Mercury Playground (browser based) or the Max8 version of the livecoding environment:

Follow the step-by-step guide here

Bug reports are welcome in the issues. If the issue is more Mercury than Flok related please report here

Development

Basic setup

Install all dependencies and build all subpackages with:

npm install
npm run build

Then, to run web server:

cd packages/web
npm run dev

To run production build:

npm start

Packages overview

This repository is a monorepo, with multiple modular packages. Each package has its own README with more information. Here is a brief overview of the packages:

App packages

  • flok-web: Web Server for Flok
  • flok-repl: REPL Client for Flok
  • flok-server: Flok server, handles sessions and communication between clients.

Lib packages

Examples

  • example-vanilla-js: Example of a Flok-based collaborative editor written in pure JS and Vite

Design constraints (v1.0)

  • Include a simplified vanilla JS example
  • Use CodeMirror 6
    • Best code editor library for the Web
    • Latest version (v6) comes with better extensibility and accesability
  • Use Yjs for collaborative editor
  • More modular and extensible, similar to CodeMirror extensions, e.g.:
    • Line/block-based evaluation: @flok-editor/cm-eval
    • TidalCycles pattern and RMS decorators: @flok-editor/cm-tidalcycles-decorators
    • TidalCycles autocompletion: @flok-editor/cm-tidalcycles-autocompletion
    • Hydra synth autocompletion: @flok-editor/cm-hydra-autocompletion
  • Better UI for customizing editor and session configuration
    • Menu, toast, dialogs
  • nice to have Import external JS libraries dynamically, instead of bundling them with Flok
    • Similar to JS playgrounds, like codesandbox.io
    • User can have their own set of libraries to be loaded automatically or easily on new sketches
    • Connect to local filesystem for files and libraries

Hash parameters

  • username (string): Default user name. Eg: #username=arbor
  • targets (list of strings): If session is empty, configure it with the specified targets by default. Eg: #targets=hydra,strudel
  • c0, c1, ..., c7 (string): Default code to load on each document/pane (if available). Code must be encoded in Base64. Eg: #c0=bm9pc2UoKS5vdXQoKQ%253D%253D (decodes to noise().out()).
  • code (string): An alias of c0 (see above)

Query parameters

  • readOnly (boolean): Disable editing. If true, it won't ask for a user name when loading.
  • bgOpacity (number): Background opacity. Valid range: [0, 1]
  • noWebEval (list of strings): Disable evaluation of the specified web targets. Useful for embedding Flok in a website, where the website already has its own evaluation mechanism. This still sends messages to parent window. Options: *, [webTarget]. Eg: ?noWebEval=hydra disables only Hydra. ?noWebEval=* disables all web targets.
  • hideErrors (boolean): Do not show errors for web targets (hydra, strudel, etc)

Window messages

Flok will post messages to the parent window on specific events. This is useful for embedding Flok in a website, where the website can handle the evaluation of the code.

Events

  • change: When the session changes. This usually happens at the beginning, when the session is empty, and when the user changes the targets.
{
  "event": "change",
  "documents": [
    {
      "id": "1",
      "target": "hydra",
      "content": "osc().out()"
    },
    {
      "id": "2",
      "target": "tidal",
      "content": "d1 $ s \"bd\""
    }
  ]
}
  • eval: On evaluation. This happens when the user presses the "Run" button or when the user presses one of the shortcuts for evaluating (e.g. Ctrl+Enter) on the editor. Only the content of the document that was evaluated is sent.
{
  "event": "eval",
  "id": "2",
  "content": "d1 silence",
  "user": "munshkr"
}

Acknowledgments

Contributing

Bug reports and pull requests are welcome on GitHub at the issues page. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

This project is licensed under GPL 3+. Refer to LICENSE.txt

Favicon based on "Origami" by Andrejs Kirma, from Noun Project (CC BY 3.0)

flok's People

Contributors

bubobubobubobubo avatar dependabot[bot] avatar diegodorado avatar emptyflash avatar felixroos avatar hatchjaw avatar jordan-gillard avatar leandroyako avatar mathigatti avatar munshkr avatar tmhglnd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flok's Issues

No module named 'FoxDot.lib'

I am a novice and it is probably my mistake but when I follow the instructions to run foxdot I get this
Traceback (most recent call last):
File "", line 1, in
File "/Users/XXXX/FoxDot/init.py", line 120, in
from .lib import *
ModuleNotFoundError: No module named 'FoxDot.lib'

Which version of Python do you recommend?

FoxDot indent error

Hi, I get errors when I try to execute multiline commands in foxdot because of indentation issues.

It looks like something like this:

def a_function():
    p1 >> pluck()

is converted into this:

def a_function():
p1 >> pluck()

There might be some issue trim in the lines? Maybe here?

Rename sclang REPL target for sclang_osc

I think sclang_osc is a better name than remote_sclang because actually it wasn't really remote (communicates with local sclang only), and the main difference is that it uses OSC instead of Pipes (as with the rest of the REPLs) to communicate with sclang.

Store sessions content on localStorage

Content should be stored on local storage automatically whenever the document changes (onChange event).

Then, on page load, if there are no other peers and the document is empty, try to load from local storage.

Pub/Sub communication is not encrypted

Now WebRTC is encrypted, but Pub/Sub communication, which is used mainly for code evalution and target messages (stdout/stderr from REPL processes), is not.

Add Minimize button on target messages pane

This button should allow the user to minimize and completely hide any message that may appear, even errors. Especially useful for users that are not interested in reading those (for example, Hydra users)

Plugins system

This would allow flok to be extended. Plugins can be installable npm packages that extend functionality.

For example, we could write a plugin for Hydra, that adds a special target "hydra" that skips Pub/Sub and directly renders in a background canvas (same as the Hydra web editor).

Reimplement Pub/Sub client and server using WebRTC with simple-peer

Pub/Sub is used for publishing code evaluation requests and target messages from REPLs, and is implemented using a WebSockets server. To make Flok truly P2P, we need to reimplement this using WebRTC, like Yjs does for sharing text editor changes to all connected peers.

Customizable layouts

The idea is to let user build the session layout (and targets). Things to customize:

  • Buffer layout
  • Number of buffers
  • Target of each buffer

how to run?

Hi.

I came here from https://forum.toplap.org/t/what-is-this-editor-for-live-collaboration/1224

the README suggests "simply run flok-web" but that isn't working - of course, as there is no such thing in my $PATH. I would have been more surprised if it did work.

I did npm install flok-web flok-repl as suggested (but without -g, to keep my global state sane). During install, I see some warnings. (Which I ignore, because it says warning, not error...)

Then this seems to work (at least ist starts):

./node_modules/.bin/flok-repl 
Missing REPL command (e.g.: flok-repl -- cat)
Usage: flok-repl [options]

but this does not:

./node_modules/.bin/flok-web 
internal/modules/cjs/loader.js:983
  throw err;
  ^

Error: Cannot find module '/home/waldmann/software/music/flok/node_modules/flok-web/.next/prerender-manifest.json'

This is on Fedora GNU/Linux with node v12.16.2 npm 6.14.4

Make it easier to start REPLs for known live coding languages and tools

Currently the only way to start a REPL is by using repl.js, which is hard to use for languages like TidalCycles that don't have a single executable script (a combination of calling ghci with some arguments and a bootstrap script included in Tidal source) or in other operating systems like Windows. Besides you need to download the repository, install Node.js and floks dependencies to run repl.js.

The goal is to have a small packaged GUI application that already knows how to run some tools and is cross-platform. This executable must be portable.

Possible solutions:

  • Use NodeGUI for building the desktop application. NodeGUI seems to have a smaller memory footprint than Electron.
  • Package application using pkg or, in the case of NodeGUI, Packer.

--redirect-https gives SSL_ERROR_RX_RECORD_TOO_LONG

I am starting the server:

./packages/web/bin/flok-web.js  -P 8000 --redirect-https
> Going to redirect http to https
> Listening on http://0.0.0.0:8000

When connect to that port with firefox (75.0), the redirect happens (the address bar shows "https://" but then I get

Error code: SSL_ERROR_RX_RECORD_TOO_LONG

(I checked that it's working without "--redirect-https", so it's not #56 )

Use Yjs instead of ShareDB

This seems to be a much simpler solution, and eventually would support peer-to-peer connections using WebRTC or maybe Hypercore (DAT protocol). We are going to try with the latest v13 version. For now using y-websocket provider and y-codemirror binding.

Here's a demo: https://yjs-demos.now.sh/codemirror/

Evaluation messages would still need a Pub/Sub server.

Adding support for Barry (web-based bytebeat interpreter)

I saw this project mentioned in TOPLAP networks, and found it interesting. I am developing a postfix bytebeat / rampcode interpreter called Barry (compiled to run in a web browser using emscripten) and was thinking about hacking etherpad to make a collaborative interface to Barry, but this seems a much better base to build on.

I've never used Typescript and my Javascript skills aren't so great, so I don't know where to start hacking. So this issue is mainly a request for a how-to guide on adding new languages.

Barry home page is at https://mathr.co.uk/barry
You can try Barry non-collaboratively here: https://mathr.co.uk/barry/v2
You can get Barry's C source code here: https://code.mathr.co.uk/barry
(note: Fridays are strike days)

It works like a REPL, but running in the browser: see the source code for the HTML to see how the textarea is connected to the C code (it's compiled to WASM with a JS interface). Ctrl-Enter does whole-program-compilation at the moment, switching to block-based eval would be essential for sane collaboration. I have some code to collect blocks and emit whole-program code already (Barrence) which shouldn't be too hard to port to run in the client or server (that would have the advantage of late-joining clients being in sync with the latest running code, which would be perfect for bytebeat as it has no memory).

Duplicate target messages when using multiple REPL clients

When all users are in remote places and each one starts a REPL client, they will all receive the same target messages multiple times (once for each user). Currently, the editor shows all messages in a random order, so it's quite confusing.

I can think of two solutions:

  • Add an option in the editor to hide messages from other users (i.e. only show messages from your REPL clients).
  • Show all messages, but grouped by user (maybe shown in "tabs" in the messages pane?)

Detect when user disconnects from session

Currently there's no way to tell for a user when another user disconnects from the session. We could implement heartbeats, but another way would be for the server to notify all clients when someone connects or disconnects by updating current state or sending an OP.

Proposal

When a client connects to the Pub/Sub server, it registers user as a client and assigns a uuid as clientId. The client registers user using this id, and the session she's in, in the users Document.

When that client disconnects, server fetches the session from the users document, and performs the following OPs:

  • delete key from users document
  • delete user from session:{id} document

Support Hydra remote streams

The Hydra editor supports connecting remote streams via a WebRTC patch bay. We could implement this here, but the API of initStream should change a bit.

initStream should receive a reference of Flok web client as parameter, so that Flok knows on which client it should evaluate the code to initialize the source, and prepare the other clients for receiving the source stream.

Simplify session creation: Make sessions invite-only

On home page, user can create a new session or join an existing one.

Create session

  1. First, Flok automatically creates an invite token for the session, stores all the session attributes locally (local storage or cookies), then goes to the editor.
  2. On the editor, Flok joins the WebRTC room and tries to store the session attributes stored on the browser in a special Yjs document. Then, it displays a modal showing the invite token, and for easier access, an invite URL for other peers. It also shows instructions on how to run flok-repl for this particular session (e.g. depending on the specified targets, the command line arguments needed).

Join session

  1. User can enter the invite token from the /join page, or join via the invite URL directly (/join?token=).
  2. After joining, Flok enters the WebRTC room and tries to retrieve the session attributes. If the document does not exists, returns 404 and redirects to home page. Otherwise, it shows basic session info (targets and enabled extensions) and asks the user for her nickname. When accepting, Flok goes to the editor
  3. On the editor, Flok displays a modal with info about how to set up flok-repl (similar to the user who created the session).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.