Making a Pear Desktop Application

This guide demonstrates how to build a peer-to-peer chat application.

It continues where Starting a Pear Desktop Project left off.

Build with Pear - Episode 01: Developing with Pear

Step 1. HTML Structure and CSS Styles

The project folder should contain:

  • package.json

  • index.html

  • app.js

  • test/index.test.js

Start by defining the app's layout in index.html:

Note: To make the <pear-ctrl> element draggable in Pear applications, wrap it in another element that uses the following CSS property:

This non-standard CSS property tells the application that this element should act as a draggable region for the entire window.

Running pear run --dev . should show

Layout of the app

Step 2. Module dependencies

Note: Close the app before installing dependencies. If dependencies are installed while the app is running, an error is thrown.

Install the development dependencies using:

This will install the following:

The app uses these modules:

Install the dependencies with:

Step 3. JavaScript

Replace app.js with

Note that the pear dependency is used, even though it was not installed. This is the Pear API, available to any Pear project.

Step 4. Chat

Open two app instances by running pear run --dev . in two terminals.

In the first app, click on Create. A random topic will appear at the top.

Note that topics consist of 64 hexadecimal characters (32 bytes).

The first app, with the topic

Paste the topic into the second app, then click on Join.

Second app, using topic from the first

Once connected, messages can be sent between each chat application.

View from the first app
View from the second app

Discussion

Chatting With Another Machine

The two application instances used Hyperswarm's distributed hash table (DHT) to connect with each other.

The DHT enables connections across different machines, so chatting with other people is also possible, as long as they run the same application.

One option is to copy the code, but it is also possible to distribute the application itself over the DHT. This is the topic of Sharing a Pear Application.

Joining Topics VS Joining Servers

In a traditional client-server setup, the server is hosted at an IP address (or hostname) and a port, e.g. http://localhost:3000. This is what clients use to connect to the server.

The code in app.js contains the line swarm.join(topicBuffer, { client: true, server: true }).

topicBuffer is the invitation: anyone who knows this topic can join the room and message the other members.

Note that all members are equal: there is no separate client or server. If the room creator goes offline, or even deletes the room from their machine, the other members can continue chatting.

Frontend Frameworks

Any frontend framework can be used with Pear.

Next

Last updated