Skip to content

Instantly share code, notes, and snippets.

Last active September 23, 2023 20:19
Show Gist options
  • Save RobbieClarken/8844828 to your computer and use it in GitHub Desktop.
Save RobbieClarken/8844828 to your computer and use it in GitHub Desktop.
EPICS over WebSockets with Flask


pip install Flask Flask-Sockets gunicorn pyepics


gunicorn -k flask_sockets.worker index:app
<!DOCTYPE html>
document.addEventListener('DOMContentLoaded', function() {
var socket = new WebSocket('ws://')
socket.onmessage = function(event) {
var data = JSON.parse(
document.getElementById('beam-current').innerHTML = data['value']
<p>Beam Current: <span id='beam-current'></span></p>
#-*- coding: utf-8 -*-
from flask import Flask, render_template
from flask_sockets import Sockets
from geventwebsocket.exceptions import WebSocketError
from epics import PV
import json
app = Flask(__name__)
sockets = Sockets(app)
ws_conns = []
def index():
return render_template('index.html')
def echo_socket(ws):
while True:
message = ws.receive()
if message is None:
def callback(pvname, value, **kws):
message = json.dumps({'pvname': pvname, 'value': value})
for ws in ws_conns:
except WebSocketError:
pv = PV('SR11BCM01:CURRENT_MONITOR', callback=callback)
Copy link

Any idea on if this would work with multiple PVs? Been trying it for shutter PV's and not having any luck. Can read them fine using pyepics but unable to get it to work in the callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment