2

So the scenario is like this... I have a number of different users in an organization. Each has his own session of an AngularJS app running in their browser. They share an internet connection over a local LAN. enter image description here

I need them to continue working together (data, notifications, ... etc) even when they lose internet i.e. server side communication.

What is the best architecture for solving this?

Jalal El-Shaer
  • 14,502
  • 8
  • 45
  • 51

2 Answers2

1

Having clients communicate directly, without a server, requires peer-to-peer connections.

If your users are updating data that should be reflected in the database, then you will have to cache that data locally on the client until the server is available again. But if you want to first send that data to other peers, then you need to think carefully about which client will then update the database when the server comes back up (should it be the original client that made the edit - who may not be online anymore - or should it be the first client that establishes server connection). Lots to consider in your architecture.

To cope with this scenario you need angular service-worker library, which you can read about here.

If you just want the clients/users to communicate without persisting data in the database (eg. simple chat messages) then you don't have to worry about the above complexity.

Refer to this example which shows how to use simple-peer library with Angular2.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • I believe this is a very valid architecture... thou not easy to implement. Yes each originator shall be responsible for updating the database with their copy of the data. So it is a kind of distributed database that communicates and syncs back to another server. Then we have to define the "team" that is allowed to peer together ... mmm ... sounds good candidate for a library – Jalal El-Shaer Feb 23 '18 at 12:55
  • Indeed that library does solve this architectural problem - but how you would use it with angular I am not so sure about! But nice find, thanks for the link :) – rmcsharry Feb 23 '18 at 17:26
  • I wonder if there is something similar that works in the browser ... has anything similar passed by you ? – Jalal El-Shaer Feb 23 '18 at 20:18
  • Gun does work in the browser, it's all just javascript. I don't know of any alternatives that provide both P2P and database persistence. But you could start looking here: https://github.com/kgryte/awesome-peer-to-peer – rmcsharry Feb 24 '18 at 11:31
1

An assisting answer (doesn't fit in a comment) was provided here: https://github.com/amark/gun/issues/506

Here is it: Since GUN can connect to multiple peers, you can have the browser connect to both outside/external servers AND peers running on your local area network. All you have to do is npm install gun and then npm start it on a few machines within your LAN and then hardcode/refresh/update their local IPs in the browser app (perhaps could even use GUN to do that, by storing/syncing a table of local IPs as the update/change)

Ideally we would all use WebRTC and have our browsers connect to each other directly. This is possible however has a big problem, WebRTC depends upon a relay/signal server every time the browser is refreshed. This is kinda stupid and is the browser/WebRTC's fault, not GUN (or other P2P systems). So either way, you'd have to also do (1) either way.

If you are on the same computer, in the same browser, in the same browser session, it is possible to relay changes (although I didn't bother to code for this, as it is kinda useless behavior) - it wouldn't work with other machines in your LAN.

Summary: As long as you are running some local peers within your network, and can access them locally, then you can do "offline" (where offline here is referencing external/outside network) sync with GUN.

GUN is also offline-first in that, even if 2 machines are truly disconnected, if they make local edits while they are offline, they will sync properly when the machines eventually come back online/reconnect.

I hope this helps.

Jalal El-Shaer
  • 14,502
  • 8
  • 45
  • 51