GithubHelp home page GithubHelp logo

jimf99 / node-sse-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thesalarkhan/node-sse-server

0.0 0.0 0.0 61 KB

SSE Microservice, Nodejs based, Non-Blocking, PubSub

JavaScript 83.24% HTML 14.27% Shell 0.37% Dockerfile 2.12%

node-sse-server's Introduction

This is a microservice meant for streaming data via SSE. It uses the PUB/SUB pattern.

For an explanation of SSE watch this video: https://www.youtube.com/watch?v=71hId_-Iwqc

One can poteltially also use this as an online presence microservice, as the server keeps a list of connected clients, we can choose to stream that as another sse endpoint, or we can have a REST api endpoint for querying the information of a certain client, or get a list of all the connected clientIds.

Example

npm install

$ npm i

Start the dev server

$ npm run dev

Subscribe

To subscribe use EventSource object.

<!DOCTYPE html> <html> <body>
	<script type="text/javascript">
		const channelsToSubscribe = ["channel1", "channel2"];
		const source = new EventSource(`http://localhost:9090/subscribe/?channels=${encodeURIComponent(channelsToSubscribe.join(","))}`);

		function _getEventData(event) {
			const data = JSON.parse(event.data);
			const { channelName, type, payload } = data;
			return channelName ? {
				channelName,
				type,
				payload
			} : {
				type,
				payload
			};
		}
		// Fires once on a successful subscription.
		source.addEventListener("registered", (event) => {
			console.log(_getEventData(event));
		});
		// Fires once for every subscribed channel, giving the last
		// published value on that channel or "{}"
		source.addEventListener("lastEvent", (event) => {
			console.log(_getEventData(event));
		});
		// Fires on receiving an event of eventType "message"
		// for a channel.
		source.addEventListener("message", (event) => {
			document.body.innerHTML += event.data + "<br>";
		});
		// Fires on receiving an event of eventType "foo"
		source.addEventListener("foo", (event) => {
			document.body.innerHTML += event.data + "<br>";
		});
		// Fires on receiving an event of eventType "bar"
		source.addEventListener("bar", (event) => {
			document.body.innerHTML += event.data + "<br>";
		});
	</script>
</body> </html>

Publish

To publish do the following post call. We can post to multiple channels with multiple events in a single request. The event listener corresponding to 'eventType' will be fired on all clients connected to the respective channels.

For example this request will fire the following events on channel "channel1": "message", "foo". And it will fire the following events on channel "channel2": "message", "bar".


method:
	POST
url:
	http://localhost:3000/publish
headers:
	'Content-Type': 'application/json'
body:
	{
		"events": [
			{
				"channelName": "channel1",
				"type": "message",
				"payload": "{ \"location\": \"123.44,34.234234\"}"
			},
			{
				"channelName": "channel1",
				"type": "foo",
				"payload": "{ \"message\": \"Hi fooo\"}"
			},
			{
				"channelName": "channel2",
				"type": "message",
				"payload": "{ \"location\": \"123.44,872938.99\"}"
			},
			{
				"channelName": "channel2",
				"type": "bar",
				"payload": "{ \"location\": \"123.44\"}"
			}
		]
	}

TODOS

* Security: Token or Secret based.

node-sse-server's People

Contributors

thesalarkhan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.