Connectivity

Nov 20, 2020

โš ๏ธ This is the first half of the post but it contains most of the important basic information.

A string telephone (childrens physics experiment)
Please take a moment and observe the red wire on this picture. It could be the most interesting thing you are seeing today. A simple red wire. Look at it like a child. Isn't it quite mysterious? What are all the things it is capable of? How does it actually work? What basic concepts from it could we apply to understanding the digital equivalent of a connection?

This red wire together with two tin cans is a connection. It is peer to peer, realtime and it carries serially encoded pieces of information. Information is encoded as a series of vibrations spread out in time and space.

Message order is guaranteed. It is not possible to say two things into such microphone and for the channel to mix them up, making earlier one arrive later. This is a great property to have and is usually taken for granted.

Messages in this simple analogy case are not encrypted ๐Ÿ” but this is not relevant for the journey of understanding we are just about to embark. Encryption is easily added on top of the basic design we are going to invent now.

Come on board, let's go!

Wire vs. connection

In our physical case we have these two aspects we can cleary identify:

  • a wire: a physical medium over which a connection can take place
  • a connection: a wire that connects a sender and a receiver

Sender and receiver can be instantly reversed over the same connection so it would be more accurate to say that a bidirectional connection is a wire that connects two transceiver nodes. Our physical example can only work in one direction at any given time but digital connections don't have this limitation.

A bidirectional connection connects two nodes and allows message passing in both directions.

We will start with the basic Connectome API design now.

(Connectome) Channel

Let's now move into the digital realm.

  • channel: an abstract view of both the physical medium and the connection.

From a perspective of one node it looks like this:

That's it. Our JavaScript object. All connection details are abstracted away.

It is an object exactly like this:

A Connectome channel

We have exactly one channel for each connection. It is a clear 1:1 analogy with our toy example. Analogy will break a little bit at some point but it is actually quite amazing how true it actually is.

When we have the channel, we can use it immediately, it comes ready to go.

However, once closed, we lose it forever. Like this:

A channel is never reopened after it closes. This is part of Connectome Design.

So far we talked about how channel gets destroyed (when connection drops) but how it gets created in the first place?

Channel gets created each time the other side connects to us.

Connectome channel creation

Let's go back to the basics for a minute.

Each connection consists of exactly two nodes and they can be equal in all aspects except in one.

In Connectome Design they can never be equal in the fact that only one node was initiating the connection while other was the receiver of connection. Once the connection is active it technically does not matter which is which but we still like to keep things clean and so we call the "channel" on the initiating side a connector. This is the connector object:

This is the same as "channel" but on the side which was initiating the connection.

This is the entire picture:

Creating a connection (connection initialization)

From browser:

import { connectBrowser } from 'connectome';

const connector = connectBrowser({ address, protocol, lane });

From node.js:

import { connect } from 'connectome';

const connector = connect({ address, protocol, lane });

When we do this we will have a `connector` object immediately but it may be connected already or not. We can check like this:

connector.isReady();

However we usually want to do this instead:

connector.on('ready', () => { 
  // now we are ready to send and receive messages of any kind
})

Channel creation

At almost the same instant as we get our ready event, the other side will get a completely prepared channel handed to them inside an onCreate function:

function onConnect({ channel }) {
  // we have our channel now, also ready to send and receive messages
}

We can see that both connector and channel are conceptually the same thing. They can both be treated as channel, which is a very broad concept from communication theory.

Differences that are good to know are:

  • we always have `connector` object on the side that initiated the connection
  • recipient of a successful connection attempt will be using channel object for further communication
  • channel is never used in browser because browser always initiates the connection and does not receive it
  • a connector when disconnected will keep retrying to connect
  • a channel when disconnected will be destroyed but a new channel will be created when other side reconnects successfully.

Conclusion (for now, 1/2)

More experienced programmers might ask: what is new in this design?

My answer: if you knew everything already, then nothing is new for you, if you knew almost nothing about this topic, then everything is new for you. If you never or rarely used raw WebSockets then by default this information will be new for you.

There is no such thing as "connection" in physical sense. All connections are just logical layers. Currently "open connections" have some time resolution within which the process can be certain that anything sent to the remote endpoint will indeed arrive. With every ยตs of receiving a ping message, the process can be less sure about whether the connection is "still open" or only seems so.

We will go into these details in a day or two. This post will be updated and increased in size by about 100%.

Connection as a concept is further complicated by packet switching networks (which Internet is). WebSockets abstract details away and we can think of our connection as a red wire from the first picture although it is anything but in actual reality.

Conceptually it is exactly like that though.

What can we send over it?

Everything. How? Stay tuned :)

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.