https://github.com/redis-field-engineering/redis-streams-to-ws
Serve Redis streams as a Web socket endpoint
https://github.com/redis-field-engineering/redis-streams-to-ws
Last synced: 2 months ago
JSON representation
Serve Redis streams as a Web socket endpoint
- Host: GitHub
- URL: https://github.com/redis-field-engineering/redis-streams-to-ws
- Owner: redis-field-engineering
- License: bsd-3-clause
- Created: 2021-12-06T19:53:25.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-12T15:50:30.000Z (about 3 years ago)
- Last Synced: 2025-01-25T12:09:03.406Z (4 months ago)
- Language: Go
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Redis Streams to Websockets

Example code to show how a web browser can receive all updates on a [Redis Stream](https://redis.io/topics/streams-intro) via Websockets
### Using
In your web application add the following Javascript
```javascript
var conn = new WebSocket("ws://localhost:8080/ws?lastMod=0&Stream=");
conn.onclose = function(evt) {
data.textContent = 'Connection closed';
}
conn.onmessage = function(evt) {
RunMyDisplayFunction(evt.data);
}
```The event data will be an array of JSON object mapping the the key/value pairs within the Redis stream message
```
$ redis-cli
127.0.0.1:6379> multi
OK
127.0.0.1:6379> xadd test_stream * name Neil instrument drums
QUEUED
127.0.0.1:6379> xadd test_stream * name Alex instrument guitar
QUEUED
127.0.0.1:6379> exec
1) "1639074721749-0"
2) "1639074721749-1"
```Will send the following JSON update
```json
[{"name": "Neil", "instrument": "drums"},
{"name": "Alex", "instrument": "guitar"}]
```### Testing / Example
Startup docker-compose
```
docker-compose up
```Open a [web browser](http://localhost:8080/test)
Using Redis CLI add an entry to the test stream and watch the websocket test page update
```
$ for j in {1..10}; do
redis-cli xadd test_stream "*" key value${j} count $j time `date +%s.%N` user `whoami`
sleep 1
done
```