https://github.com/vasanthv/unsocket
A HTTP-based pub-sub service to replace websockets
https://github.com/vasanthv/unsocket
Last synced: about 1 year ago
JSON representation
A HTTP-based pub-sub service to replace websockets
- Host: GitHub
- URL: https://github.com/vasanthv/unsocket
- Owner: vasanthv
- License: mit
- Created: 2023-09-21T23:41:47.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-29T13:31:30.000Z (almost 3 years ago)
- Last Synced: 2024-12-17T01:23:05.418Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://unsocket.vercel.app
- Size: 31.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unsocket
## A HTTP-based pub-sub service to replace websockets using Express.js
Unsocket uses HTTP POST to post events and Server-Sent Events (SSE) to subscribe to events. So, no third-party client is needed, all major browsers support this protocol.
### Subscribe to a channel
Unsocket uses channels to differentiate multiple streams. You can use SSE as below to subscribe to any channel.
```js
const events = new EventSource("/test-channel");
```
Here `test-channel` is the channel name. A channel name should be lowercase alphanumeric characters up to 40 characters long.
### Listening to events
You can listen to all messages in a channel or only to specific events as follows.
```js
// Listens to all messages in the channel
events.onmessage((e) => console.log(e));
// Listens to only chat events in the channel
events.addEventListener("chat", (e) => console.log(e));
```
### Posting to channel
Use simple HTTP POST for emitting events to all the subscribers. Currently, only plain text body is supported.
```js
fetch("/test-channel", {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: { "Content-Type": "text/plain" },
body: "Hello World",
});
```
### How to run locally
Fork this repo and then clone it:
```
git clone https://github.com//unsocket.git
```
```
npm install
```
Then just run
```
npm start
```
to start the development server on port `3000`. Your jsonbox instance will be running on `http://localhost:3000`.
### LICENSE
MIT