2

I am currently creating a website and I'm totally confused about Websockets.

I have some data in a database that is shown on my website. Now every once in a while there are new entries in the database, which should be shown on the website without reloading it, now I thought this could somehow be achieved using websockets.

I'm using web.py as framework for my website, and I use AngularJS.

In my app.py, I recieve the database entries and return them as JSON.

In js I want to receive the JSON message and save it in the $scope, which then gets "printed" on the website using AngularJS and I created a client side WebSocket for it like this:

var app = angular.module('web');

app.factory('runservice', function() {
  var service = {};
  service.connect = function() {
    if(service.ws) { return; }

    var ws = new WebSocket('wss://localhost:8080');
    ws.onopen = function() {
      service.callback("Success");
    };

    ws.onerror = function(evt) {
      service.callback("Error: " + evt.data);
    }

    ws.onmessage = function(message) {
      service.callback(message.data);
    };

    service.ws = ws;

  }
  service.subscribe = function(callback) {
    service.callback = callback;
  }
  return service;
});


app.controller('runController', function($scope, runservice) {
  runservice.connect();
  runservice.subscribe(function(message) {
    var data = JSON.parse(message);
    $scope.runs = data;
  });
});

Now, do I need a server side socket in my app.py or something else? If so, can anyone provide an example how I'd achieve this in web.py?

1 Answer 1

2

You definitely need to have websocket code on your server, or else your client isn't keeping a connection alive with your server and vice versa.

If your wish is to make use of realtime websockets, then this package for your web.py application server https://github.com/songdi/webpy-socketio will be very useful, as will this for you angular client application https://github.com/btford/angular-socket-io

Another option would be to simply long poll your server. AKA make asynchronous requests every ~10 seconds or so to your application and retrieve only the newest entries.

I hope this is of some help!

Sign up to request clarification or add additional context in comments.

4 Comments

I've once tried the 10 second request, but I think its an ugly solution which is why I went for websockets, I will check out both projects suggested, thank you
I'm still a little confused, if you open a certain webpage, web.py would automatically execute the pages logic (the according class), which is where I return the JSON data. Would I have to return the JSON data as a message to a client (e.g. socket.send(message)) or can I still return it normally as I used to?
Hey, not totally sure I understand the question... On initial load, you would return your response as usual from the server. Once the socket is established between the client and the server, you can then send messages from the server to the client across the socket. Which, from the docs would be something like webpy_socketio.send(session_id, message)
I've adjusted my app.py according to the "running" part in the socketio readme on github, and now I get a 404 for every file (/static/*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.