2

I am trying to understand how to use websockets correctly and seem to be missing some fundamental part of the puzzle.

Say I have a website with 3 different pages:

  • newsfeed1.html
  • newsfeed2.html
  • newsfeed3.html

When a user goes to one of those pages they get a feed specific to the page, ie newsfeed1.html = sport, newsfeed2.html = world news etc.

There is a CoreApplication.py that does all the handling of getting data and parsing etc.

Then there is a WebSocketServer.py, using say Autobahn.

All the examples I have looked at, and that is alot, only seem to react to a message from the client (browser) within the WebSocketServer.py, think chat echo examples. So a client browser sends a chat message and it is echoed back or broadcast to all connected client browsers.

What I am trying to figure out is given the following two components:

  • CoreApplication.py

  • WebSocketServer.py

How to best make CoreApplication.py communicate with WebSocketServer.py for the purpose of sending messages to connected users.

Normally should CoreApplication.py simply send command messages to the WebSocketServer.py as a client. For example like this:

CoreApplication.py -> Connects to WebServerSocket.py as a normal client -> sends a Json command message (like broadcast message X to all users || send message Y to specific remote client) -> WebSocketServer.py determines how to process the incoming message dependant on which client is connected to which feed and sends to according remote client browsers.

OR, should CoreApplication.py connect programatically with WebSocketServer.py? As I cannot seem to find any examples of being able to do this for example with Autobahn or other simple web sockets as once the WebSocketServer is instantiated it seems to run in a loop and does not accept external sendMessage requests?

So to sum up the question: What is the best practice? To simply make CoreApplication.py interact with WebSocketServer.py as a client (with special command data) or for CoreApplication.py to use an already running instance of WebSocketServer.py (both of which are on the same machine) through some more direct method to directly sendMessages without having to make a full websocket connection first to the WebSocketServer.py server?

YakovL
  • 7,557
  • 12
  • 62
  • 102
someuser
  • 2,279
  • 4
  • 28
  • 39

1 Answers1

0

It depends on your software design, if you decide the logic from WebSocketServer.px and CoreApplication.py belongs together, merge it.

If not, you need some kind of inter process communication (ipc). You can use websockets for this ipc, but i would suggest, you use something simpler. For example, you can you json-rpc over tcp or unix domain to send control messages from CoreApplication.py to WebSocketServer.py

flotto
  • 535
  • 5
  • 17
  • Thanks. The problem is I have been having alot of trouble even trying to get WebSocketServer.py to have a callable method to send messages as can be seen in more detail here: http://stackoverflow.com/questions/29955373/autobahn-with-sendmessage-using-threading-and-twisted Which led me to think maybe I was approaching the problem in the wrong way. – someuser Apr 30 '15 at 12:47
  • Hmm, i guess you have general problems with this "event driven" programming stuff? i'm not familiar with python and twisted, buf if you like, i will post a example using node.js – flotto Apr 30 '15 at 16:48