diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-10-29 14:41:39 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-10-29 14:41:39 +0100 |
| commit | 2836163e54e3b94342113314e70ee564c456c43e (patch) | |
| tree | 59b82fc69e83cc6d92846a8e9f510b0bb865cf3b /public/simple-server-sent-events-based-pubsub-server.html | |
| parent | d50ea4053ea04abb3a455606d4591a8283af0677 (diff) | |
| download | mitjafelicijan.com-2836163e54e3b94342113314e70ee564c456c43e.tar.gz | |
Added public folder to git so it get get deployed on vercel
Diffstat (limited to 'public/simple-server-sent-events-based-pubsub-server.html')
| -rwxr-xr-x | public/simple-server-sent-events-based-pubsub-server.html | 324 |
1 files changed, 324 insertions, 0 deletions
diff --git a/public/simple-server-sent-events-based-pubsub-server.html b/public/simple-server-sent-events-based-pubsub-server.html new file mode 100755 index 0000000..19f3dda --- /dev/null +++ b/public/simple-server-sent-events-based-pubsub-server.html | |||
| @@ -0,0 +1,324 @@ | |||
| 1 | <!doctype html><html lang=en-us><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL69vf8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv76+/8LBwQkAAAAAAAAAAAAAAAC+vb3/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL+9vf/Bv78JAAAAAAAAAAAAAAAAu7q6/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC7ubr/vr29CAAAAAAAAAAAy8nJAZ6foP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAnqGj/6GipAoAAAAAHLjU/xcXHf/BwsL/I8XY/yPK3v8XGiD/IbjL/yPF2f8XGiD/Fxkf/yLF2f8gnK3/Fxog/62ztv8fwNf/FRcd/x271v8mz93/GRsi/xkXHf8p097/GiIp/xobIv8p0t3/KdPe/xocIv8fYmr/KNPe/xoZH/8aHCL/J87c/xy81/8VFxz/IsPZ/8zS0/8XGiD/Ir/R/yPH2/8XGiD/Fxkf/yPH2/8dd4T/GBog/yPJ3f8jyNr/uru9/xcUGv8cudb/EhITDKi5vRKlvMP/RUpOERwcHRAdOj4QHTk8EBwdHRAdNTgQHTo/EBwcHRAcHB0QSGduEKW4vf+koqQfHzg+EBqz0ewSFRv7EyMr/xq51vsTERb7ExUb+xq41fsau9j7ExUb+xiPp/sZudb7ExUb+xMVG/sZuNX/GKvI/BIUGfMdvdn/IrfL/xcaIP8n1eb/J9Dh/xkcIf8ZGR7/J8/f/xxCSv8ZGyH/J9Dg/ybQ4P8ZHCL/FSQs/yPK3/8UExj/GE1b/ybS5P8ZGB7/Ghwj/ynW5P8p2Ob/Ghwi/yWrtv8p1eH/Ghwi/xocIv8p1uT/J8XT/xkcIv8m1un/Hb7d/xUYH/8hzOr/HtHu/xcaIf8XGB//I8vi/xgxOv8XGSD/I8rg/yPK4P8XGiD/GUFL/yPP6f8SERj/Fhkh/x3A4f8AAAAAJ2f9/ydr//8mZPH/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlYu38J2v//ydo/f8AAAAAAAAAAAd8/fkFqf//Iob8sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMY39awWr//8FfP3/AAAAAAAAAAAFm/7/SfD//wR+/f8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOB/f9B7v//BaX+/wAAAAAAAAAAQ878SAyZ/v9n1v4KAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADu9v8DDJb+/z3N/XgAAAAA3/sAAN/7AADf+wAA3/sAAAAAAAAAAAAAAAAAAN/7AAAAAAAAAAAAAAAAAAAAAAAAj/EAAI/5AACP8QAA3/sAAA==" rel=icon type=image/x-icon><title>Simple Server-Sent Events based PubSub Server</title><meta name=description content="Before we continue ."><link rel=alternate type=application/rss+xml title="Mitja Felicijan's posts" href=https://mitjafelicijan.com/index.xml><link rel=alternate type=application/rss+xml title="Mitja Felicijan's notes" href=https://mitjafelicijan.com/notes.xml><style>body{padding:1rem;max-width:760px;background:#fff;font-family:sans-serif;line-height:1.35rem;font-size:16px;margin:0 auto}hr{margin-block-start:1.5rem}h1,h2,h3{line-height:initial}h1{font-size:xx-large}footer{margin-block-start:2rem}cap{text-transform:capitalize}table{max-width:100%;width:100%;border-collapse:separate;border-spacing:2px;border:1px solid #000;border-left:1px solid #999;border-top:1px solid #999}blockquote{font-style:italic}table thead{background:#eee}ul.list li{padding:.2em 0}ul{line-height:1.4em}td,th{border:1px solid #000;padding:4px;border-right:1px solid #999;border-bottom:1px solid #999;text-align:left}pre{text-wrap:nowrap;overflow-x:auto;padding:0 1em;border:1px solid #dcdcdc}code{padding:0 3px;font-size:14px;border:0}pre code{line-height:1.3em}pre,code,pre *,code *{font-family:monospace}figure{margin-inline-start:0;margin-inline-end:0}figcaption{text-align:center}figcaption p{margin:.3em 0 0}img,video,audio{max-width:100%}header{display:flex;flex-direction:row;gap:3rem}nav{display:flex;gap:.75rem}nav.main{flex-grow:1}.pstatus-orange{background:gold}.pstatus-green{background:#9acd32}.pstatus-red{background:#cd5c5c}@media only screen and (max-width:600px){body{padding:15px}header{flex-direction:column;gap:1rem}a{word-wrap:break-word}}</style><header><nav class=main itemscope itemtype=http://schema.org/SiteNavigationElement role=toolbar><a href=/>Home</a> | ||
| 2 | <a href=https://git.mitjafelicijan.com/ target=_blank>Git</a> | ||
| 3 | <a href=https://files.mitjafelicijan.com/ target=_blank>Files</a> | ||
| 4 | <a href=/radio.pls target=_blank>Radio</a> | ||
| 5 | <a href=/mitjafelicijan.pgp.pub.txt target=_blank>PGP</a> | ||
| 6 | <a href=/curriculum-vitae.html>CV</a> | ||
| 7 | <a href=/index.xml target=_blank>RSS</a></nav></header><main role=main><article itemtype=http://schema.org/Article><h1 itemtype=headline>Simple Server-Sent Events based PubSub Server</h1><p><cap>post</cap>, Mar 22, 2020 on <a href=https://mitjafelicijan.com>Mitja Felicijan's blog</a><div><h2 id=before-we-continue->Before we continue ...</h2><p>Publisher Subscriber model is nothing new and there are many amazing solutions | ||
| 8 | out there, so writing a new one would be a waste of time if other solutions | ||
| 9 | wouldn't have quite complex install procedures and weren't so hard to maintain. | ||
| 10 | But to be fair, comparing this simple server with something like | ||
| 11 | <a href=https://kafka.apache.org/>Kafka</a> or <a href=https://www.rabbitmq.com/>RabbitMQ</a> is | ||
| 12 | laughable at the least. Those solutions are enterprise grade and have many | ||
| 13 | mechanisms there to ensure messages aren't lost and much more. Regardless of | ||
| 14 | these drawbacks, this method has been tested on a large website and worked until | ||
| 15 | now without any problems. So now, that we got that cleared up, let's continue.<p><em><strong>Wiki definition:</strong> Publish/subscribe messaging, or pub/sub messaging, is a | ||
| 16 | form of asynchronous service-to-service communication used in serverless and | ||
| 17 | microservices architectures. In a pub/sub model, any message published to a | ||
| 18 | topic is immediately received by all the subscribers to the topic.</em><h2 id=general-goals>General goals</h2><ul><li>provide a simple server that relays messages to all the connected clients,<li>messages can be posted on specific topics,<li>messages get sent via <a href=https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events>Server-Sent | ||
| 19 | Events</a> | ||
| 20 | to all the subscribers.</ul><h2 id=how-exactly-does-the-pubsub-model-work>How exactly does the pub/sub model work?</h2><p>The easiest way to explain this is with diagram bellow. Basic function is | ||
| 21 | simple. We have subscribers that receive messages, and we have publishers that | ||
| 22 | create and post messages. Similar model is also well know pattern that works on | ||
| 23 | a premise of consumers and producers, and they take similar roles.<figure><img src=/posts/simple-pubsub-server/pubsub-overview.png alt="How PubSub works"></figure><p><strong>These are some naive characteristics we want to achieve:</strong><ul><li>producer is publishing messages to subscribe topic,<li>consumer is receiving messages from subscribed topic,<li>servers is also known as Broker,<li>broker does not store messages or tracks success,<li>broker uses | ||
| 24 | <a href=https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)>FIFO</a> method | ||
| 25 | for delivering messages,<li>if consumer wants to receive messages from a topic, producer and consumer | ||
| 26 | topics must match,<li>consumer can subscribe to multiple topics,<li>producer can publish to multiple topics,<li>each message has a messageId.</ul><p><strong>Known drawbacks:</strong><ul><li>messages will not be stored in a persistent queue or unreceived messages like | ||
| 27 | <a href=https://en.wikipedia.org/wiki/Dead_letter_queue>DeadLetterQueue</a> so old | ||
| 28 | messages could be lost on server restart,<li><a href=https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events>Server-Sent | ||
| 29 | Events</a> | ||
| 30 | opens a long-running connection between the client and the server so make sure | ||
| 31 | if your setup is load balanced that the load balancer in this case can have | ||
| 32 | long opened connection,<li>no system moderation due to the dynamic nature of creating queues.</ul><h2 id=server-sent-events>Server-Sent Events</h2><p>Read more about it on <a href=https://html.spec.whatwg.org/multipage/server-sent-events.html>official specification | ||
| 33 | page</a>.<h3 id=current-browser-support>Current browser support</h3><figure><img src=/posts/simple-pubsub-server/caniuse.png alt="Browser support"></figure><p>Check | ||
| 34 | <a href="https://caniuse.com/#feat=eventsource">https://caniuse.com/#feat=eventsource</a> | ||
| 35 | for latest information about browser support.<h3 id=known-issues>Known issues</h3><ul><li>Firefox 52 and below do not support EventSource in web/shared workers<li>In Firefox prior to version 36 server-sent events do not reconnect | ||
| 36 | automatically in case of a connection interrupt (bug)<li>Reportedly, CORS in EventSource is currently supported in Firefox 10+, Opera | ||
| 37 | 12+, Chrome 26+, Safari 7.0+.<li>Antivirus software may block the event streaming data chunks.</ul><p>Source: <a href="https://caniuse.com/#feat=eventsource">https://caniuse.com/#feat=eventsource</a><h3 id=message-format>Message format</h3><p>The simplest message that can be sent is only with data attribute:<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>data: this is a simple message | ||
| 38 | </span></span><span style=display:flex><span><blank line> | ||
| 39 | </span></span></code></pre><p>You can send message IDs to be used if the connection is dropped:<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>id: 33 | ||
| 40 | </span></span><span style=display:flex><span>data: this is line one | ||
| 41 | </span></span><span style=display:flex><span>data: this is line two | ||
| 42 | </span></span><span style=display:flex><span><blank line> | ||
| 43 | </span></span></code></pre><p>And you can specify your own event types (the above messages will all trigger | ||
| 44 | the message event):<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>id: 36 | ||
| 45 | </span></span><span style=display:flex><span>event: price | ||
| 46 | </span></span><span style=display:flex><span>data: 103.34 | ||
| 47 | </span></span><span style=display:flex><span><blank line> | ||
| 48 | </span></span></code></pre><h3 id=server-requirements>Server requirements</h3><p>The important thing is how you send headers and which headers are sent by the | ||
| 49 | server that triggers browser to threat response as a EventStream.<p>Headers responsible for this are:<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>Content-Type: text/event-stream | ||
| 50 | </span></span><span style=display:flex><span>Cache-Control: no-cache | ||
| 51 | </span></span><span style=display:flex><span>Connection: keep-alive | ||
| 52 | </span></span></code></pre><h3 id=debugging-with-google-chrome>Debugging with Google Chrome</h3><p>Google Chrome provides build-in debugging and exploration tool for <a href=https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events>Server-Sent | ||
| 53 | Events</a> | ||
| 54 | which is quite nice and available from Developer Tools under Network tab.<blockquote><p>You can debug only client side events that get received and not the server | ||
| 55 | ones. For debugging server events add <code>console.log</code> to <code>server.js</code> code and | ||
| 56 | print out events.</blockquote><figure><img src=/posts/simple-pubsub-server/chrome-debugging.png alt="Google Chrome Developer Tools EventStream"></figure><h2 id=server-implementation>Server implementation</h2><p>For the sake of this example we will use <a href=https://nodejs.org/en/>Node.js</a> with | ||
| 57 | <a href=https://expressjs.com>Express</a> as our router since this is the easiest way to | ||
| 58 | get started and we will use already written SSE library for node | ||
| 59 | <a href=https://www.npmjs.com/package/sse-pubsub>sse-pubsub</a> so we don't reinvent the | ||
| 60 | wheel.<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>npm init --yes | ||
| 61 | </span></span><span style=display:flex><span> | ||
| 62 | </span></span><span style=display:flex><span>npm install express | ||
| 63 | </span></span><span style=display:flex><span>npm install body-parser | ||
| 64 | </span></span><span style=display:flex><span>npm install sse-pubsub | ||
| 65 | </span></span></code></pre><p>Basic implementation of a server (<code>server.js</code>):<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span><span style=color:#00f>const</span> express = require(<span style=color:#a31515>'express'</span>); | ||
| 66 | </span></span><span style=display:flex><span><span style=color:#00f>const</span> bodyParser = require(<span style=color:#a31515>'body-parser'</span>); | ||
| 67 | </span></span><span style=display:flex><span><span style=color:#00f>const</span> SSETopic = require(<span style=color:#a31515>'sse-pubsub'</span>); | ||
| 68 | </span></span><span style=display:flex><span> | ||
| 69 | </span></span><span style=display:flex><span><span style=color:#00f>const</span> app = express(); | ||
| 70 | </span></span><span style=display:flex><span><span style=color:#00f>const</span> port = process.env.PORT || 4000; | ||
| 71 | </span></span><span style=display:flex><span> | ||
| 72 | </span></span><span style=display:flex><span><span style=color:green>// topics container | ||
| 73 | </span></span></span><span style=display:flex><span><span style=color:green></span><span style=color:#00f>const</span> sseTopics = {}; | ||
| 74 | </span></span><span style=display:flex><span> | ||
| 75 | </span></span><span style=display:flex><span>app.use(bodyParser.json()); | ||
| 76 | </span></span><span style=display:flex><span> | ||
| 77 | </span></span><span style=display:flex><span><span style=color:green>// open for all cors | ||
| 78 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.all(<span style=color:#a31515>'*'</span>, (req, res, next) => { | ||
| 79 | </span></span><span style=display:flex><span> res.header(<span style=color:#a31515>'Access-Control-Allow-Origin'</span>, <span style=color:#a31515>'*'</span>); | ||
| 80 | </span></span><span style=display:flex><span> res.header(<span style=color:#a31515>'Access-Control-Allow-Headers'</span>, <span style=color:#a31515>'X-Requested-With, Content-Type'</span>); | ||
| 81 | </span></span><span style=display:flex><span> next(); | ||
| 82 | </span></span><span style=display:flex><span>}); | ||
| 83 | </span></span><span style=display:flex><span> | ||
| 84 | </span></span><span style=display:flex><span><span style=color:green>// preflight request error fix | ||
| 85 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.options(<span style=color:#a31515>'*'</span>, <span style=color:#00f>async</span> (req, res) => { | ||
| 86 | </span></span><span style=display:flex><span> res.header(<span style=color:#a31515>'Access-Control-Allow-Origin'</span>, <span style=color:#a31515>'*'</span>); | ||
| 87 | </span></span><span style=display:flex><span> res.header(<span style=color:#a31515>'Access-Control-Allow-Headers'</span>, <span style=color:#a31515>'X-Requested-With, Content-Type'</span>); | ||
| 88 | </span></span><span style=display:flex><span> res.send(<span style=color:#a31515>'OK'</span>); | ||
| 89 | </span></span><span style=display:flex><span>}); | ||
| 90 | </span></span><span style=display:flex><span> | ||
| 91 | </span></span><span style=display:flex><span><span style=color:green>// serve the event streams | ||
| 92 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.get(<span style=color:#a31515>'/stream/:topic'</span>, <span style=color:#00f>async</span> (req, res, next) => { | ||
| 93 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> topic = req.params.topic; | ||
| 94 | </span></span><span style=display:flex><span> | ||
| 95 | </span></span><span style=display:flex><span> <span style=color:#00f>if</span> (!(topic <span style=color:#00f>in</span> sseTopics)) { | ||
| 96 | </span></span><span style=display:flex><span> sseTopics[topic] = <span style=color:#00f>new</span> SSETopic({ | ||
| 97 | </span></span><span style=display:flex><span> pingInterval: 0, | ||
| 98 | </span></span><span style=display:flex><span> maxStreamDuration: 15000, | ||
| 99 | </span></span><span style=display:flex><span> }); | ||
| 100 | </span></span><span style=display:flex><span> } | ||
| 101 | </span></span><span style=display:flex><span> | ||
| 102 | </span></span><span style=display:flex><span> <span style=color:green>// subscribing client to topic | ||
| 103 | </span></span></span><span style=display:flex><span><span style=color:green></span> sseTopics[topic].subscribe(req, res); | ||
| 104 | </span></span><span style=display:flex><span>}); | ||
| 105 | </span></span><span style=display:flex><span> | ||
| 106 | </span></span><span style=display:flex><span><span style=color:green>// accepts new messages into topic | ||
| 107 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.post(<span style=color:#a31515>'/publish'</span>, <span style=color:#00f>async</span> (req, res) => { | ||
| 108 | </span></span><span style=display:flex><span> <span style=color:#00f>let</span> body = req.body; | ||
| 109 | </span></span><span style=display:flex><span> <span style=color:#00f>let</span> status = 200; | ||
| 110 | </span></span><span style=display:flex><span> | ||
| 111 | </span></span><span style=display:flex><span> console.log(<span style=color:#a31515>'Incoming message:'</span>, req.body); | ||
| 112 | </span></span><span style=display:flex><span> | ||
| 113 | </span></span><span style=display:flex><span> <span style=color:#00f>if</span> ( | ||
| 114 | </span></span><span style=display:flex><span> body.hasOwnProperty(<span style=color:#a31515>'topic'</span>) && | ||
| 115 | </span></span><span style=display:flex><span> body.hasOwnProperty(<span style=color:#a31515>'event'</span>) && | ||
| 116 | </span></span><span style=display:flex><span> body.hasOwnProperty(<span style=color:#a31515>'message'</span>) | ||
| 117 | </span></span><span style=display:flex><span> ) { | ||
| 118 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> topic = req.body.topic; | ||
| 119 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> event = req.body.event; | ||
| 120 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> message = req.body.message; | ||
| 121 | </span></span><span style=display:flex><span> | ||
| 122 | </span></span><span style=display:flex><span> <span style=color:#00f>if</span> (topic <span style=color:#00f>in</span> sseTopics) { | ||
| 123 | </span></span><span style=display:flex><span> <span style=color:green>// sends message to all the subscribers | ||
| 124 | </span></span></span><span style=display:flex><span><span style=color:green></span> sseTopics[topic].publish(message, event); | ||
| 125 | </span></span><span style=display:flex><span> } | ||
| 126 | </span></span><span style=display:flex><span> } <span style=color:#00f>else</span> { | ||
| 127 | </span></span><span style=display:flex><span> status = 400; | ||
| 128 | </span></span><span style=display:flex><span> } | ||
| 129 | </span></span><span style=display:flex><span> | ||
| 130 | </span></span><span style=display:flex><span> res.status(status).send({ | ||
| 131 | </span></span><span style=display:flex><span> status, | ||
| 132 | </span></span><span style=display:flex><span> }); | ||
| 133 | </span></span><span style=display:flex><span>}); | ||
| 134 | </span></span><span style=display:flex><span> | ||
| 135 | </span></span><span style=display:flex><span><span style=color:green>// returns JSON object of all opened topics | ||
| 136 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.get(<span style=color:#a31515>'/status'</span>, <span style=color:#00f>async</span> (req, res) => { | ||
| 137 | </span></span><span style=display:flex><span> res.send(sseTopics); | ||
| 138 | </span></span><span style=display:flex><span>}); | ||
| 139 | </span></span><span style=display:flex><span> | ||
| 140 | </span></span><span style=display:flex><span><span style=color:green>// health-check endpoint | ||
| 141 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.get(<span style=color:#a31515>'/'</span>, <span style=color:#00f>async</span> (req, res) => { | ||
| 142 | </span></span><span style=display:flex><span> res.send(<span style=color:#a31515>'OK'</span>); | ||
| 143 | </span></span><span style=display:flex><span>}); | ||
| 144 | </span></span><span style=display:flex><span> | ||
| 145 | </span></span><span style=display:flex><span><span style=color:green>// return a 404 if no routes match | ||
| 146 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.use((req, res, next) => { | ||
| 147 | </span></span><span style=display:flex><span> res.set(<span style=color:#a31515>'Cache-Control'</span>, <span style=color:#a31515>'private, no-store'</span>); | ||
| 148 | </span></span><span style=display:flex><span> res.status(404).end(<span style=color:#a31515>'Not found'</span>); | ||
| 149 | </span></span><span style=display:flex><span>}); | ||
| 150 | </span></span><span style=display:flex><span> | ||
| 151 | </span></span><span style=display:flex><span><span style=color:green>// starts the server | ||
| 152 | </span></span></span><span style=display:flex><span><span style=color:green></span>app.listen(port, () => { | ||
| 153 | </span></span><span style=display:flex><span> console.log(<span style=color:#a31515>`PubSub server running on http://localhost:</span><span style=color:#a31515>${</span>port<span style=color:#a31515>}</span><span style=color:#a31515>`</span>); | ||
| 154 | </span></span><span style=display:flex><span>}); | ||
| 155 | </span></span></code></pre><h3 id=our-custom-message-format>Our custom message format</h3><p>Each message posted on a server must be in a specific format that out server | ||
| 156 | accepts. Having structure like this allows us to have multiple separated type of | ||
| 157 | events on each topic.<p>With this we can separate streams and only receive events that belong to the | ||
| 158 | topic.<p>One example would be, that we have index page and we want to receive messages | ||
| 159 | about new upvotes or new subscribers but we don't want to follow events for | ||
| 160 | other pages. This reduces clutter and overall network. And structure is much | ||
| 161 | nicer and maintanable.<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span>{ | ||
| 162 | </span></span><span style=display:flex><span> "topic": <span style=color:#a31515>"sample-topic"</span>, | ||
| 163 | </span></span><span style=display:flex><span> "event": <span style=color:#a31515>"sample-event"</span>, | ||
| 164 | </span></span><span style=display:flex><span> "message": { "name": <span style=color:#a31515>"John"</span> } | ||
| 165 | </span></span><span style=display:flex><span>} | ||
| 166 | </span></span></code></pre><h2 id=publisher-and-subscriber-clients>Publisher and subscriber clients</h2><h3 id=publisher-and-subscriber-in-action>Publisher and subscriber in action</h3><p><video src=/posts/simple-pubsub-server/clients.m4v controls></video><p>You can download <a href=../simple-pubsub-server/sse-pubsub-server.zip>the code</a> and | ||
| 167 | follow along.<h3 id=publisher>Publisher</h3><p>As talked about above publisher is the one that send messages to the | ||
| 168 | broker/server. Message inside the payload can be whatever you want (string, | ||
| 169 | object, array). I would however personally avoid send large chunks of data like | ||
| 170 | blobs and such.<pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span><span style=color:#00f><!DOCTYPE html></span> | ||
| 171 | </span></span><span style=display:flex><span><html lang=<span style=color:#a31515>"en"</span>> | ||
| 172 | </span></span><span style=display:flex><span> | ||
| 173 | </span></span><span style=display:flex><span> <head> | ||
| 174 | </span></span><span style=display:flex><span> <meta charset=<span style=color:#a31515>"UTF-8"</span>> | ||
| 175 | </span></span><span style=display:flex><span> <meta name=<span style=color:#a31515>"viewport"</span> content=<span style=color:#a31515>"width=device-width, initial-scale=1.0"</span>> | ||
| 176 | </span></span><span style=display:flex><span> <title>Publisher</title> | ||
| 177 | </span></span><span style=display:flex><span> </head> | ||
| 178 | </span></span><span style=display:flex><span> | ||
| 179 | </span></span><span style=display:flex><span> <body> | ||
| 180 | </span></span><span style=display:flex><span> | ||
| 181 | </span></span><span style=display:flex><span> <h1>Publisher</h1> | ||
| 182 | </span></span><span style=display:flex><span> | ||
| 183 | </span></span><span style=display:flex><span> <fieldset> | ||
| 184 | </span></span><span style=display:flex><span> <p> | ||
| 185 | </span></span><span style=display:flex><span> <label>Server:</label> | ||
| 186 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"server"</span> value=<span style=color:#a31515>"http://localhost:4000"</span>> | ||
| 187 | </span></span><span style=display:flex><span> </p> | ||
| 188 | </span></span><span style=display:flex><span> <p> | ||
| 189 | </span></span><span style=display:flex><span> <label>Topic:</label> | ||
| 190 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"topic"</span> value=<span style=color:#a31515>"sample-topic"</span>> | ||
| 191 | </span></span><span style=display:flex><span> </p> | ||
| 192 | </span></span><span style=display:flex><span> <p> | ||
| 193 | </span></span><span style=display:flex><span> <label>Event:</label> | ||
| 194 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"event"</span> value=<span style=color:#a31515>"sample-event"</span>> | ||
| 195 | </span></span><span style=display:flex><span> </p> | ||
| 196 | </span></span><span style=display:flex><span> <p> | ||
| 197 | </span></span><span style=display:flex><span> <label>Message:</label> | ||
| 198 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"message"</span> value=<span style=color:#a31515>'{"name": "John"}'</span>> | ||
| 199 | </span></span><span style=display:flex><span> </p> | ||
| 200 | </span></span><span style=display:flex><span> <p> | ||
| 201 | </span></span><span style=display:flex><span> <button type=<span style=color:#a31515>"button"</span> id=<span style=color:#a31515>"button"</span>>Publish message to topic</button> | ||
| 202 | </span></span><span style=display:flex><span> </p> | ||
| 203 | </span></span><span style=display:flex><span> </fieldset> | ||
| 204 | </span></span><span style=display:flex><span> | ||
| 205 | </span></span><span style=display:flex><span> <script> | ||
| 206 | </span></span><span style=display:flex><span> | ||
| 207 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> button = document.querySelector(<span style=color:#a31515>'#button'</span>); | ||
| 208 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> server = document.querySelector(<span style=color:#a31515>'#server'</span>); | ||
| 209 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> topic = document.querySelector(<span style=color:#a31515>'#topic'</span>); | ||
| 210 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> event = document.querySelector(<span style=color:#a31515>'#event'</span>); | ||
| 211 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> message = document.querySelector(<span style=color:#a31515>'#message'</span>); | ||
| 212 | </span></span><span style=display:flex><span> | ||
| 213 | </span></span><span style=display:flex><span> button.addEventListener(<span style=color:#a31515>'click'</span>, <span style=color:#00f>async</span> (evt) => { | ||
| 214 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> req = <span style=color:#00f>await</span> fetch(<span style=color:#a31515>`</span><span style=color:#a31515>${</span>server.value<span style=color:#a31515>}</span><span style=color:#a31515>/publish`</span>, { | ||
| 215 | </span></span><span style=display:flex><span> method: <span style=color:#a31515>'post'</span>, | ||
| 216 | </span></span><span style=display:flex><span> headers: { | ||
| 217 | </span></span><span style=display:flex><span> <span style=color:#a31515>'Accept'</span>: <span style=color:#a31515>'application/json'</span>, | ||
| 218 | </span></span><span style=display:flex><span> <span style=color:#a31515>'Content-Type'</span>: <span style=color:#a31515>'application/json'</span>, | ||
| 219 | </span></span><span style=display:flex><span> }, | ||
| 220 | </span></span><span style=display:flex><span> body: JSON.stringify({ | ||
| 221 | </span></span><span style=display:flex><span> topic: topic.value, | ||
| 222 | </span></span><span style=display:flex><span> event: event.value, | ||
| 223 | </span></span><span style=display:flex><span> message: JSON.parse(message.value), | ||
| 224 | </span></span><span style=display:flex><span> }), | ||
| 225 | </span></span><span style=display:flex><span> }); | ||
| 226 | </span></span><span style=display:flex><span> | ||
| 227 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> res = <span style=color:#00f>await</span> req.json(); | ||
| 228 | </span></span><span style=display:flex><span> console.log(res); | ||
| 229 | </span></span><span style=display:flex><span> }); | ||
| 230 | </span></span><span style=display:flex><span> | ||
| 231 | </span></span><span style=display:flex><span> </script> | ||
| 232 | </span></span><span style=display:flex><span> | ||
| 233 | </span></span><span style=display:flex><span> </body> | ||
| 234 | </span></span><span style=display:flex><span> | ||
| 235 | </span></span><span style=display:flex><span></html> | ||
| 236 | </span></span></code></pre><h3 id=subscriber>Subscriber</h3><p>Subscriber is responsible for receiving new messages that come from server via | ||
| 237 | publisher. The code bellow is very rudimentary but works and follows the | ||
| 238 | implementation guidelines for EventSource.<p>You can use either Developer Tools Console to see incoming messages or you can | ||
| 239 | defer to Debugging with Google Chrome section above to see all EventStream | ||
| 240 | messages.<blockquote><p>Don't be alarmed if the subscriber gets disconnected from the server every so | ||
| 241 | often. The code we have here resets connection every 15s but it automatically | ||
| 242 | get reconnected and fetches all messages up to last received message id. This | ||
| 243 | setting can be adjusted in <code>server.js</code> file; search for the | ||
| 244 | <code>maxStreamDuration</code> variable.</blockquote><pre tabindex=0 style=background-color:#fff><code><span style=display:flex><span><span style=color:#00f><!DOCTYPE html></span> | ||
| 245 | </span></span><span style=display:flex><span><html lang=<span style=color:#a31515>"en"</span>> | ||
| 246 | </span></span><span style=display:flex><span> | ||
| 247 | </span></span><span style=display:flex><span> <head> | ||
| 248 | </span></span><span style=display:flex><span> <meta charset=<span style=color:#a31515>"UTF-8"</span>> | ||
| 249 | </span></span><span style=display:flex><span> <meta name=<span style=color:#a31515>"viewport"</span> content=<span style=color:#a31515>"width=device-width, initial-scale=1.0"</span>> | ||
| 250 | </span></span><span style=display:flex><span> <title>Subscriber</title> | ||
| 251 | </span></span><span style=display:flex><span> <link rel=<span style=color:#a31515>"stylesheet"</span> href=<span style=color:#a31515>"style.css"</span>> | ||
| 252 | </span></span><span style=display:flex><span> </head> | ||
| 253 | </span></span><span style=display:flex><span> | ||
| 254 | </span></span><span style=display:flex><span> <body> | ||
| 255 | </span></span><span style=display:flex><span> | ||
| 256 | </span></span><span style=display:flex><span> <h1>Subscriber</h1> | ||
| 257 | </span></span><span style=display:flex><span> | ||
| 258 | </span></span><span style=display:flex><span> <fieldset> | ||
| 259 | </span></span><span style=display:flex><span> <p> | ||
| 260 | </span></span><span style=display:flex><span> <label>Server:</label> | ||
| 261 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"server"</span> value=<span style=color:#a31515>"http://localhost:4000"</span>> | ||
| 262 | </span></span><span style=display:flex><span> </p> | ||
| 263 | </span></span><span style=display:flex><span> <p> | ||
| 264 | </span></span><span style=display:flex><span> <label>Topic:</label> | ||
| 265 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"topic"</span> value=<span style=color:#a31515>"sample-topic"</span>> | ||
| 266 | </span></span><span style=display:flex><span> </p> | ||
| 267 | </span></span><span style=display:flex><span> <p> | ||
| 268 | </span></span><span style=display:flex><span> <label>Event:</label> | ||
| 269 | </span></span><span style=display:flex><span> <input type=<span style=color:#a31515>"text"</span> id=<span style=color:#a31515>"event"</span> value=<span style=color:#a31515>"sample-event"</span>> | ||
| 270 | </span></span><span style=display:flex><span> </p> | ||
| 271 | </span></span><span style=display:flex><span> <p> | ||
| 272 | </span></span><span style=display:flex><span> <button type=<span style=color:#a31515>"button"</span> id=<span style=color:#a31515>"button"</span>>Subscribe to topic</button> | ||
| 273 | </span></span><span style=display:flex><span> </p> | ||
| 274 | </span></span><span style=display:flex><span> </fieldset> | ||
| 275 | </span></span><span style=display:flex><span> | ||
| 276 | </span></span><span style=display:flex><span> <script> | ||
| 277 | </span></span><span style=display:flex><span> | ||
| 278 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> button = document.querySelector(<span style=color:#a31515>'#button'</span>); | ||
| 279 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> server = document.querySelector(<span style=color:#a31515>'#server'</span>); | ||
| 280 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> topic = document.querySelector(<span style=color:#a31515>'#topic'</span>); | ||
| 281 | </span></span><span style=display:flex><span> <span style=color:#00f>const</span> event = document.querySelector(<span style=color:#a31515>'#event'</span>); | ||
| 282 | </span></span><span style=display:flex><span> | ||
| 283 | </span></span><span style=display:flex><span> button.addEventListener(<span style=color:#a31515>'click'</span>, <span style=color:#00f>async</span> (evt) => { | ||
| 284 | </span></span><span style=display:flex><span> | ||
| 285 | </span></span><span style=display:flex><span> <span style=color:#00f>let</span> es = <span style=color:#00f>new</span> EventSource(<span style=color:#a31515>`</span><span style=color:#a31515>${</span>server.value<span style=color:#a31515>}</span><span style=color:#a31515>/stream/</span><span style=color:#a31515>${</span>topic.value<span style=color:#a31515>}</span><span style=color:#a31515>`</span>); | ||
| 286 | </span></span><span style=display:flex><span> | ||
| 287 | </span></span><span style=display:flex><span> es.addEventListener(event.value, <span style=color:#00f>function</span> (evt) { | ||
| 288 | </span></span><span style=display:flex><span> console.log(<span style=color:#a31515>`incoming message`</span>, JSON.parse(evt.data)); | ||
| 289 | </span></span><span style=display:flex><span> }); | ||
| 290 | </span></span><span style=display:flex><span> | ||
| 291 | </span></span><span style=display:flex><span> es.addEventListener(<span style=color:#a31515>'open'</span>, <span style=color:#00f>function</span> (evt) { | ||
| 292 | </span></span><span style=display:flex><span> console.log(<span style=color:#a31515>'connected'</span>, evt); | ||
| 293 | </span></span><span style=display:flex><span> }); | ||
| 294 | </span></span><span style=display:flex><span> | ||
| 295 | </span></span><span style=display:flex><span> es.addEventListener(<span style=color:#a31515>'error'</span>, <span style=color:#00f>function</span> (evt) { | ||
| 296 | </span></span><span style=display:flex><span> console.log(<span style=color:#a31515>'error'</span>, evt); | ||
| 297 | </span></span><span style=display:flex><span> }); | ||
| 298 | </span></span><span style=display:flex><span> | ||
| 299 | </span></span><span style=display:flex><span> }); | ||
| 300 | </span></span><span style=display:flex><span> | ||
| 301 | </span></span><span style=display:flex><span> </script> | ||
| 302 | </span></span><span style=display:flex><span> | ||
| 303 | </span></span><span style=display:flex><span> </body> | ||
| 304 | </span></span><span style=display:flex><span> | ||
| 305 | </span></span><span style=display:flex><span></html> | ||
| 306 | </span></span></code></pre><h2 id=reading-further>Reading further</h2><ul><li><a href=https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events>Using server-sent events</a><li><a href=https://www.smashingmagazine.com/2018/02/sse-websockets-data-flow-http2/>Using SSE Instead Of WebSockets For Unidirectional Data Flow Over HTTP/2</a><li><a href=https://apifriends.com/api-streaming/server-sent-events/>What is Server-Sent Events?</a><li><a href=https://tools.ietf.org/id/draft-xie-bidirectional-messaging-01.html>An HTTP/2 extension for bidirectional messaging communication</a><li><a href=https://developers.google.com/web/fundamentals/performance/http2>Introduction to HTTP/2</a><li><a href=https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API>The WebSocket API (WebSockets)</a></ul></div></article></main><section><hr><h2>Posts from blogs I follow around the net</h2><ul><li><a href=https://chotrin.org/writing/2023-10-20.html target=_blank rel=noopener>OpenBSD upgrade and fall things.</a><div>Been AFK for a bit. It's autumn and I upgraded this server to OpenBSD 7.4! — <a href=https://chotrin.org>chötrin's wiki.</a><li><a href=https://mirzapandzo.com/next-image-url-parameter-is-valid-but-upstream-response-is-invalid target=_blank rel=noopener>Next/Image "url" parameter is valid but upstream response is invalid</a><div>Getting "url" parameter is valid but upstream response is invalid error with Next/Image on WSL2 — <a href=https://mirzapandzo.com/>Mirza Pandzo's Blog</a><li><a href=https://drewdevault.com/2023/10/13/Going-off-script.html target=_blank rel=noopener>Going off-script</a><div>There is a phenomenon in society which I find quite bizarre. Upon our entry to | ||
| 307 | this mortal coil, we are endowed with self-awareness, agency, and free will. | ||
| 308 | Each of th… — <a href=https://drewdevault.com>Drew DeVault's blog</a><li><a href=https://solar.lowtechmagazine.com/2023/10/workshop-in-rotterdam-how-to-build-a-bike-generator/ target=_blank rel=noopener>Workshop in Rotterdam: How to Build a Bike Generator</a><div>Afbeelding: Low-tech Magazine workshop in Rotterdam, the Netherlands. Poster: Marie Verdeil. Image: Sara Vercauteren | ||
| 309 | The workshop takes place on behalf of the “Hou… — <a href=https://solar.lowtechmagazine.com/posts/>LOW←TECH MAGAZINE English</a><li><a href="http://offbeatpursuit.com:80/blog/?id=24" target=_blank rel=noopener>Printf debugging</a><div>tags: | ||
| 310 | plan9 | ||
| 311 | There’s no shame in that. Yes, there is documentation, code to be | ||
| 312 | read, and debuggers to be used. But sometimes you just need to “see” | ||
| 313 | what is happening. | ||
| 314 | So… — <a href=http://offbeatpursuit.com:80/blog/>WLOG - blog</a><li><a href=https://neil.computer/notes/chart-of-accounts-for-startups-and-saas-companies/ target=_blank rel=noopener>Chart of Accounts for Startups and SaaS Companies</a><div>Accounting is fundamental to starting a business. You need to have a basic understanding of accounting principles and essential bookkeeping. I had to learn it. Ther… — <a href=https://neil.computer/>Neil Panchal</a><li><a href=https://journal.valeriansaliou.name/deploy-a-nomad-cluster-on-alpine-linux-with-vultr/ target=_blank rel=noopener>Deploy a Nomad Cluster on Alpine Linux with Vultr</a><div>After spending countless hours trying to understand how to deploy my apps on Kubernetes for the first time to host Mirage, an AI API service that I run, I ended up … — <a href=https://journal.valeriansaliou.name/>Valerian Saliou</a><li><a href=https://jcs.org/2023/10/17/wikipedia target=_blank rel=noopener>Wikipedia Reader 1.0 Released</a><div>Wikipedia Reader | ||
| 315 | 1.0 has been released: | ||
| 316 | wikipedia-1.0.sit | ||
| 317 | (StuffIt 3 archive, includes | ||
| 318 | source code | ||
| 319 | and THINK C 5 project file) | ||
| 320 | SHA256: 360e12d064f6579695f1e627ce34cb2f0… — <a href=https://jcs.org/>joshua stein</a></ul><p><a href=https://git.sr.ht/~sircmpwn/openring>Generated with openring.</a></section><footer><hr><p><big><strong>Want to comment or have something to add?</strong></big><p>You can write me an email | ||
| 321 | at <a href=mailto:m@mitjafelicijan.com>m@mitjafelicijan.com</a> or | ||
| 322 | catch up with me <a href=https://telegram.me/mitjafelicijan target=_blank>on Telegram</a>.<hr><p>This website does not track you. Content is made available under | ||
| 323 | the <a href=https://creativecommons.org/licenses/by/4.0/ target=_blank rel=noreferrer>CC BY 4.0 license</a> unless specified | ||
| 324 | otherwise. Blog is also available as <a href=/index.xml target=_blank>RSS feed</a>.</footer> \ No newline at end of file | ||
