Using the Websocket protocol, users can be notified in (near) realtime of new events. A Json message is sent when an event is inserted or updated. Websocket protocol can be integrated into your application using different programming language.
Last messages
Javascript Code sample
<html>
<head>
<script src="js/sockjs-0.3.min.js"></script>
<script>
var sock = new SockJS('https://www.seismicportal.eu/standing_order');
sock.onopen = function() {
console.log('connected');
};
sock.onmessage = function(e) {
msg = JSON.parse(e.data);
console.log('message received : ', msg);
};
sock.onclose = function() {
console.log('disconnected');
};
</script>
</head>
<body>
see your console log
</body>
</html>
Python Code sample
from__future__import unicode_literals
fromtornado.websocketimport websocket_connect
fromtornado.ioloopimport IOLoop
fromtornadoimport gen
importloggingimportjsonimportsys
echo_uri = 'wss://www.seismicportal.eu/standing_order/websocket'
PING_INTERVAL = 15
#You can modify this function to run custom process on the messagedef myprocessing(message):
try:
data = json.loads(message)
info = data['data']['properties']
info['action'] = data['action']
logging.info('>>>> {action:7} event from {auth:7}, unid:{unid}, T0:{time}, Mag:{mag}, Region: {flynn_region}'.format(**info))
exceptException:
logging.exception("Unable to parse json message")
@gen.coroutine
def listen(ws):
while True:
msg = yield ws.read_message()
if msg is None:
logging.info("close")
self.ws = None
break
myprocessing(msg)
@gen.coroutine
def launch_client():
try:
logging.info("Open WebSocket connection to %s", echo_uri)
ws = yield websocket_connect(echo_uri, ping_interval=PING_INTERVAL)
exceptException:
logging.exception("connection error")
else:
logging.info("Waiting for messages...")
listen(ws)
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
ioloop = IOLoop.instance()
launch_client()
try:
ioloop.start()
exceptKeyboardInterrupt:
logging.info("Close WebSocket")
ioloop.stop()