Commit df2314fc authored by Jed Simson's avatar Jed Simson

Testing out WebSockets

parent 6f9b1e10
from flask import Blueprint, request, jsonify, render_template
from flask_socketio import SocketIO, emit
from lib.slider import Slider
import time
......@@ -7,20 +9,46 @@ import time
api = Blueprint('api', __name__, url_prefix='/api')
slider = Slider()
socketio = SocketIO()
def index():
return jsonify({'name:': 'Reddit Slider',
'description': 'A simple program to allow greater filtering options for Reddit submissions.',
'version': 1.0})
def test():
return render_template('test.html')
@socketio.on('connect', namespace='/api')
def test_connect():
print('Received connect')
emit('response', {'data': 'Connected'})
@socketio.on('message event', namespace='/api')
def test_message(message):
print('Received message')
emit('response', {'data': 'Received message: ' + str(message['data'])})
@socketio.on('disconnect', namespace='/api')
def test_disconnect():
print('Received disconnect')
print('Client disconnected')
@socketio.on('search event', namespace='/api')
def search_event(subreddit):
def subreddit(subreddit):
def search(subreddit):
dimension = request.args.get('range', None)
delta = request.args.get('delta', None)
sort = request.args.get('sort', 'hot')
limit = int(request.args.get('limit', 100))
sliding_window = request.args.get('use_sliding_window', False)
sliding_window = True if sliding_window == 'true' else False
t0 = time.time()
......@@ -29,8 +57,7 @@ def subreddit(subreddit):
except ValueError as e:
t1 = time.time()
t = round(t1 - t0, 4)
from flask import Flask
from api.views import api
from api.views import api, socketio
from slider.views import slider
import os
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
......@@ -12,4 +18,5 @@ def create_app():
if __name__ == "__main__":
app = create_app()'', port=5050, debug=True)'', port=5050, debug=True)
......@@ -95,7 +95,16 @@ class Search:
r = Reddit('Reddit Slider by /u/oracular_demon - v{}'.format(self.version))
generator = submissions_between(r, subreddit, low_timestamp, high_timestamp)
submissions = []
submissions = [submission for submission in generator]
sorted_submissions = sorted(submissions, key=self.sort_fns[sort])
current = 0
while current < min(limit, len(sorted_submissions)):
current += 1
yield sorted_submissions[current]
count = 0
while count <= limit:
......@@ -113,6 +122,7 @@ class Search:
for submission in sorted_submissions:
yield submission
<title>Flask-SocketIO Test</title>
<script type="text/javascript" src="//"></script>
<script type="text/javascript" src="//"></script>
<script type="text/javascript" charset="utf-8">
namespace = '/api'; // change to an empty string to use the global namespace
// the documentation recommends sending an explicit package upon connection
// this is specially important when using the global namespace
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
// event handler for server sent data
// the data is displayed in the "Received" section of the page
socket.on('response', function(msg) {
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.count + ': ' +;
// event handler for new connections
socket.on('connect', function() {
socket.emit('message event', {data: 'I\'m connected!'});
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
socket.emit('message event', {data: $('#emit_data').val()});
return false;
$('form#disconnect').submit(function(event) {
return false;
<form id="emit" method="POST" action='#'>
<input type="text" name="emit_data" id="emit_data" placeholder="Message">
<input type="submit" value="Echo">
<form id="disconnect" method="POST" action="#">
<input type="submit" value="Disconnect">
<div id="log"></div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment