Logo

dev-resources.site

for different kinds of informations.

System Design: Hybrid WebApp using server sent event

Published at
4/5/2024
Categories
systemdesign
realtime
Author
mossymoo
Categories
2 categories in total
systemdesign
open
realtime
open
Author
8 person written this
mossymoo
open
System Design: Hybrid WebApp using server sent event

What is a server sent event?

A client subscribes to a “stream” from a server and the server will send messages (“event-stream”) to the client until the server or the client closes the stream

Why such system design?
I am required to design a system architecture that requires on-prem WebApp to be able to interact with Cloud Web App.

Cloud Web App will act as a SAAS service for on-prem WebApp. Each on-prem WebApp is unique for each building. I did the design using websocket and Apache Pulsar but realized that although these two were able to solve the problem, it took a lot more time and ended up dragging the deadline.

Why do I choose a server sent event instead of websocket or publish subscribe mechanism like Apache Kafka or Apache Pulsar?

  1. Complexity on implementing
  2. Complexity of hiring expertise
  3. Complexity on distribution (Human Training)
  4. Lack of team member (There only 2 of us)

Diagram
Image description

Laravel route code

Route::get('/sse/{id}', function ($id) {
    $random_string = chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90));
    $data = [
      'message' => $random_string,
      'name' => 'Sadhan Sarker',
      'time' => date('h:i:s'),
      'id' => $id,
    ];

    $response = new StreamedResponse();
    $response->setCallback(function () use ($data){
      echo 'data: ' . json_encode($data) . "\n\n";
      ob_flush();
      flush();
      usleep(200000);   

    });
    $response->headers->set('Content-Type', 'text/event-stream');
    $response->headers->set('X-Accel-Buffering', 'no');
    $response->headers->set('Cach-Control', 'no-cache');
    $response->headers->set('Access-Control-Allow-Origin', '*');
    $response->send();
});
Enter fullscreen mode Exit fullscreen mode

Nodejs EventSource client

var ess = require('event-source-stream')

ess('http://mydomain.com/sse/1122')
  .on('data', function(data) {
    console.log('received event:', data)
  })
Enter fullscreen mode Exit fullscreen mode

NGINX Configuration

server {
    listen 80;
    server_name mydomain.com;
    root /var/www/mydomain.com/wee/public;

    index index.html index.htm index.php;

    location / {
        ## try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }
}
Enter fullscreen mode Exit fullscreen mode

Benefits on using server sent event

  1. Reduce complexity of entire system design. (Compared with websocket and pubsub)
  2. Easier and faster to code.
  3. Not require prior knowledge on websocket or pubsub.
  4. Less changes on software distribution. (If you're in a desktop software business, even a slight changes will cause a lot of problems especially if you're dealing with non technical peoples)

Note that this blog post is meant to help you on designing your system better and give you a sense on where to start. The codes are merely just for education and demonstration purposes.Feel free to code based on your use case.

Peace out :)

realtime Article's
30 articles in total
Favicon
Real-Time Voice Interactions with the WebSocket Audio Adapter
Favicon
Curiosity: Using Ably.io Realtime Messaging as a Lightweight Database
Favicon
Real-Time Voice Interactions over WebRTC
Favicon
Building a Real-Time Collaborative Text Editor with Slate.js
Favicon
Chat API pricing: Comparing MAU and per-minute consumption models
Favicon
Scaling Kafka with WebSockets
Favicon
Build a Real-Time Voting System with Strapi & Instant DB: Part 2
Favicon
WebSocket architecture best practices: Designing scalable realtime systems
Favicon
Build a Real-Time Voting System with Strapi & Instant DB: Part 1
Favicon
Ingesting F1 Telemetry UDP real-time data in AWS EKS
Favicon
Make a real-time, offline first application with Instant
Favicon
Tennis Australia relies on Ably to deliver live experiences for millions of tennis fans worldwide
Favicon
OctoPalm.js || JavaScript library to add real-time, customizable search to your web applications.
Favicon
Building a "Real-Time" Data Integration Platform on AWS
Favicon
Implementing Real-Time Updates with Server-Sent Events (SSE) in C# .NET: A Complete Guide
Favicon
Understanding the Importance of Kafka in High-Volume Data Environments
Favicon
Building Real-Time Applications with SignalR in .NET
Favicon
Not All Market Research Studies Need to Have Real-Time/Live Data Reporting!
Favicon
Real-Time Capabilities in API Integration
Favicon
Migrate from Cord to SuperViz
Favicon
Harnessing Firebase in React with Custom Hooks: A Practical Guide
Favicon
laravel reverb installation process and setup with common mistakes
Favicon
🚀 Want to Boost Your Sports Development? Discover the Benefits of Real-Time Results with SportDevs!
Favicon
Authenticate Realtime Pub/Sub WebSocket clients with Supabase
Favicon
Reliably syncing database and frontend state: A realtime competitor analysis
Favicon
Webhooks: A Mindset Change for Batch Jobs
Favicon
Building a Real-Time Messaging Platform with Kafka
Favicon
Real-Time Data Handling with Firestore: Tracking Pending Orders
Favicon
System Design: Hybrid WebApp using server sent event
Favicon
Real-Time Irish Transit Analytics

Featured ones: