Mesh Module


A protocol() object which is used by default in the mesh module

class js2p.mesh.mesh_connection(sock, server, outgoing)

This is the class for mesh connection abstractraction. It inherits from js2p.base.base_connection()

  • sock – This is the raw socket object
  • server (js2p.mesh.mesh_socket) – This is a link to the mesh_socket() parent
  • outgoing – This bool describes whether server initiated the connection
js2p.mesh.mesh_connection.send(msg_type, packs, id, time)

Sends a message through its connection.

  • msg_type – Message type, corresponds to the header in a pathfinding_message() object
  • packs – A list of Buffer-like objects, which correspond to the packets to send to you
  • id – The ID this message should appear to be sent from (default: your ID)
  • time (number) – The time this message should appear to be sent from (default: now in UTC)



This method is called when the expected amount of data is received

js2p.mesh.mesh_connection.handle_waterfall(msg, packets)

This method determines whether this message has been previously received or not. If it has been previously received, this method returns true. If it is older than a preset limit, this method returns true. Otherwise this method returns undefined, and forwards the message appropriately.


true or undefined


This function is run when a connection is closed


This function is run when a connection is ended

class js2p.mesh.mesh_socket(addr, port[, protocol[, out_addr[, debug_level]]])

This is the class for mesh network socket abstraction. It inherits from js2p.base.base_socket()

  • addr (string) – The address you’d like to bind to
  • port (number) – The port you’d like to bind to
  • protocol (js2p.base.protocol) – The subnet you’re looking to connect to
  • out_addr (array) – Your outward-facing address
  • debug_level (number) – The verbosity of debug prints

An object which contains mesh_connection() s keyed by their IDs


An array which contains mesh_connection() s that are awaiting handshake information


This is an array of all outgoing connections. The length of this array is used to determine whether the “socket” should automatically initiate connections


This function has two behaviors depending on whether num is truthy.

If num is truthy, it will return a list of message() objects up to length len.

If num is not truthy, it will return either a single message() object, or undefined

  • num (number) – The maximum number of message() s you would like to pull

A list of message() s, an empty list, a single message() , or undefined


Shortcut method to send a handshake response. This method is extracted from :js:meth:`~js2p.mesh.mesh_socket.__handle_handshake` in order to allow cleaner inheritence from js2p.sync.sync_socket()

js2p.mesh.mesh_socket.connect(addr, port[, id])

This function connects you to a specific node in the overall network. Connecting to one node should connect you to the rest of the network, however if you connect to the wrong subnet, the handshake failure involved is silent. You can check this by looking at the truthiness of this objects routing table. Example:

> var conn = new mesh.mesh_socket('localhost', 4444);
> conn.connect('localhost', 5555);
> //do some other setup for your program
> if (!conn.routing_table)    {
... conn.connect('localhost', 6666); // any fallback address
... }
  • addr (string) – A string address
  • port (number) – A positive, integral port
  • id – A string-like object which represents the expected ID of this node


While in the Python version there are more thorough checks on this, the Javascript implementation can connect to itself. There are checks to keep this from happening automatically, but it’s still trivial to override this via human intervention. Please do not try to connect to yourself.


Closes a given connection, and removes it from your routing tables


This function is used to generate a list-formatted group of your peers. It goes in format [ [[addr, port], ID], ...]

Returns:An array in the above format
js2p.mesh.mesh_socket.__handle_handshake(msg, conn)

This callback is used to deal with handshake signals. Its three primary jobs are:

  • reject connections seeking a different network
  • set connection state
  • deal with connection conflicts

Either true or undefined

js2p.mesh.mesh_socket.__handle_peers(msg, conn)

This callback is used to deal with peer signals. Its primary jobs is to connect to the given peers, if this does not exceed js2p.mesh.max_outgoing


Either true or undefined

js2p.mesh.mesh_socket.__handle_response(msg, conn)

This callback is used to deal with response signals. Its two primary jobs are:

  • if it was your request, send the deferred message
  • if it was someone else’s request, relay the information

Either true or undefined

js2p.mesh.mesh_socket.__handle_request(msg, conn)

This callback is used to deal with request signals. Its three primary jobs are:

  • respond with a peers signal if packets[1] is '*'
  • if you know the ID requested, respond to it
  • if you don’t, make a request with your peers

Either true or undefined

js2p.mesh.mesh_socket.send(packets[, flag[, type]])

This sends a message to all of your peers. If you use default values it will send it to everyone on the network

  • packets – A list of strings or Buffer-like objects you want your peers to receive
  • flag – A string or Buffer-like object which defines your flag. In other words, this defines packet 0.
  • type – A string or Buffer-like object which defines your message type. Changing this from default can have adverse effects.


If you change the type attribute from default values, bad things could happen. It MUST be a value from js2p.base.flags , and more specifically, it MUST be either broadcast or whisper. The only other valid flags are waterfall and renegotiate, but these are RESERVED and must NOT be used.


This function cleans the list of recently relayed messages based on the following heurisitics:

  • Delete all duplicates
  • Delete all older than 60 seconds

This function handles message relays. Its return value is based on whether it took an action or not.


true if the message was then forwarded. false if not.