Last active
August 29, 2022 02:12
-
-
Save vhxs/940f3d5adb38ae6d2e0ac4cc1ac7d185 to your computer and use it in GitHub Desktop.
simplest nontrivial example I could come up with of using websockets to push data from server to client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *really* simple example of browser client subscribing the reddit comments via websocket | |
// this is how a server can push state to a web client | |
// things like push notification etc | |
// use create-react-app to create a React application | |
// replace src/App.js with the contents of this file | |
// then run the app with `npm start` and `ws_server.py` running | |
// and open a web console. You should see reddit comments printed live in the console | |
// ripped and modified from here: https://stackoverflow.com/a/60161181 | |
import logo from './logo.svg'; | |
import './App.css'; | |
import React, {useEffect, useRef, useState} from 'react'; | |
function App() { | |
const [isPaused, setPause] = useState(false); | |
const ws = useRef(null); | |
useEffect(() => { | |
ws.current = new WebSocket("ws://localhost:8765"); | |
ws.current.onopen = () => console.log("ws opened"); | |
ws.current.onclose = () => console.log("ws closed"); | |
const wsCurrent = ws.current; | |
return () => { | |
wsCurrent.close(); | |
}; | |
}, []); | |
useEffect(() => { | |
if (!ws.current) return; | |
ws.current.onmessage = e => { | |
if (isPaused) return; | |
const message = e.data; | |
console.log(message); | |
}; | |
}, [isPaused]); | |
return ( | |
<div> | |
<button onClick={() => setPause(!isPaused)}> | |
{isPaused ? "Resume" : "Pause"} | |
</button> | |
</div> | |
); | |
} | |
export default App; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
async def hello(): | |
uri = "ws://localhost:8765" | |
async with websockets.connect(uri, ping_interval=None) as websocket: | |
while True: | |
data = await websocket.recv() | |
print(f"pushed from server: {data}") | |
asyncio.get_event_loop().run_until_complete(hello()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
import praw | |
import os | |
reddit = praw.Reddit( | |
client_id=os.environ['REDDIT_ID'], | |
client_secret=os.environ['REDDIT_SECRET'], | |
password=os.environ['REDDIT_PASS'], | |
user_agent="this is me testing praw", | |
username="vsaraph" | |
) | |
subreddit = reddit.subreddit("news") | |
async def hello(websocket, path): | |
for comment in subreddit.stream.comments(skip_existing=True): | |
await websocket.send(comment.body) | |
start_server = websockets.serve(hello, "localhost", 8765, ping_interval=None) | |
asyncio.get_event_loop().run_until_complete(start_server) | |
asyncio.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment