{
  "$type": "site.standard.document",
  "canonicalUrl": "https://unnecessary.tech/posts/whispers",
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreicwbswuweitwwxgkm7xiv7bcm7s57h4jkafqtabcm553v6emxrizu"
    },
    "mimeType": "image/png",
    "size": 120530
  },
  "description": "I used Bluesky's Jetstream and gleam to resurrect and old project I made for twitter.",
  "path": "/posts/whispers",
  "publishedAt": "2024-11-20T15:27:54.000Z",
  "site": "at://did:plc:jx54v4rmscfwzit7fmgz24ba/site.standard.publication/3mnrsqmzz3w2e",
  "tags": [
    "programming",
    "social",
    "nostalgia",
    "gleam"
  ],
  "textContent": "A long time ago twitter had some fun APIs, and I made a project\ncalled Whispers in the Dark. It took a \nreduced version of the twitter firehose\nand put up recent tweets on the screen, one after the other, but eventually\nthe sampled firehose disappeared and API access started to cost money, so\nI let Whispers in the Dark go.\n\nSince the election though, Bluesky has really taken off. It\nnow has over 20 million users and more than 3 million daily active users. \nBluesky also is a very open platform with an underlying protocol\nthat includes a firehose\nwhich is opened to users. The firehose is fairly heavy duty, imposing a pretty\nlarge network and computational load as it aims to fully synchronize remote\nservers with the state of the Bluesky servers. I don't need such a heavy duty\nsolution for Whispers in the Dark, but Bluesky also offers a lightweight\nsolution to the firehose.\n\nThe Bluesky Jetstream is a websocket\nprotocol which delivers a stream of JSON encoded events from Bluesky, including\nposts by users. This is perfect to make a new Whispers in the Dark.\nIt also gave me an opportunity to make small project using a new programming\nlanguage called Gleam.\n\nWhat is Gleam?\n\n> Gleam is a friendly language for building type-safe systems that scale!\n\n\nGleam is a functional language that runs on the BEAM),\nbut also can compile to javascript. It is a typed, functional language which\ncan be an adjustment for people who have only used procedural or object oriented\nlanguages. Gleam data structures are immutable, there are no for loops, and there\nare no exceptions. The language designer, Louis Pilfold\nborrowed from a number of languages, including Erlang, Rust, and Go, to create\na simple, friendly language that someone can learn in an afternoon, and it\nlives up to that promise. The language tour is all\nyou need to get up and productive in the language, though knowing details\nof the BEAM can be helpful.\n\nI find that it lives up to this promise of being simple, and I actually enjoy\nwritting Gleam code. The language tools seem very mature and the compiler is\nvery helpful. Examples in the language tour are good, and the code is very\nreadable. I decided that Whispers in the Dark could\nbe a good project to cut my teeth with the language. The code is posted on\ngithub\n\nThe Standard and Other Libraries\n\nA language is not syntax alone. A standard library\nis important, but there are a lot of disagreements over what standard should mean.\nGleam has a rather spare standard library, but what is there works well. There\nis also already a great ecosystem of public libraries which can be found through\nthe awesome gleam repository or\nthrough the gleam packages site.\n\nWhispers uses a number of public libraries, all of which seem very reliable and\neasy to use. The libraries I used are described below.\n\n- The Stratus websocket client library is what I\n  use to connect to the jetstream and load the posts. I still need to debug what\n  happens when the websocket unexpectedly disconnects, but so far I haven't had\n  the opportunity to do that because the connection is rock solid.\n- The Wisp web framework runs the frontend.\n  It is easy to understand to anyone who has used a small web framework such as\n  Python's Flask, or Go's\n  internal http server. \n- The Nakai library can generate HTML in Gleam.\n  I hadn't used a library like this in the past, but I find Nakai\n  is very intuitive and in the context of this app is a simpler way to produce\n  dynamic HTML. Dynamic HTML is particularly important here as I use htmx\n  on the frontend.\n\na result I had to set it to reconnect after a delay.\n\nAll these libraries, for a language that just hit 1.0 this year, are impressive\nand made it very easy to develop Whispers in the Dark.\n\nConcurrency on the Erlang Open Telecom Platform (OTP)\n\nThe BEAM was designed to run massively scalable telecommunication platforms. It\nis where Gleam inherits its ability to work at scale, and central to this is the\nOpen Telecom Platform (OTP). I am just learning about the BEAM, but the concurrency\nmodel Gleam encourages is to use actors\nwhich can receive and reply to messages, and maintain state. The central\ncomponent of an actor is a function which given a message and an incoming state\nproduces a new state and potentially a reply to that message. Messages are\nconsumed one by one in a ordered queue. Since there is no shared memory and\nonly immutable data structures are passed around, this makes it easier to reason\nabout how multiple actors can interact. These actors run as lightweight processes\nwithin the BEAM.\n\nThe Stratus library, and the Wisp framework both work using actors (or possibly\nsomewhat more complex processes) to respond to websocket messages, or web\nevents. In order to bridge the gap between the incoming Bluesky posts on the websocket\nand the outgoing posts provided by the webserver is an actor I wrote called\nholder which holds the most recent message provided by the websocket and sends\nthat to any receiver that asks for it. The code for the actor\nis very consise, and I don't have to worry about any locking when I get a new\npost from the websocket or when I send a post back to the web frontend. This\nmeans that the concurrency really is almost effortless.\n\nSummary\n\nWriting Whispers in the Dark again was a\nlot of fun. I am still thinking of making some tweaks to the presentation layer,\nbut it's shown that Gleam is very capable. The community\nis also very welcoming. I look forward to seeing how the\nlanguage progresses.",
  "title": "Whispers in the Dark: Gleam Edition"
}