Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate my Rest Api Server with my Web Socket server

Here is my problem that is partially resolved but not in the best way I think. I have a REST API Server that has a method POST that only executes a batch process. The method immediately returns 200 (without any body) and the batch process continues its task in the server. This batch process has a logging file, that I usually see online with tail -f mylogfile.log. What I want now is to use a websocket server to send the output of the tail process to webpage. The code of the websocket server look like this:

import os
import asyncio
import websockets
import subprocess

from frontserv.start_server import WEBSOCKET_INTERCHANGE_FILE


async def main(websocket, path):
    print("-> STARTING")
    exists = os.path.isfile(WEBSOCKET_INTERCHANGE_FILE)
    if not exists:
        f = open(WEBSOCKET_INTERCHANGE_FILE, "w+")
        f.close()

    exe = ["tail", "-f", WEBSOCKET_INTERCHANGE_FILE]
    message = ""
    p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=None)
    while True:
        retcode = p.poll()
        print(message)
        message = message + p.stdout.readline().decode("utf-8") + "<br>"
        await websocket.send(message)
        await asyncio.sleep(0.1)


start_server = websockets.serve(main, '127.0.0.1', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I have access to the batch process code, and I think there is a better way to resolve this. If instead of logging to a file I could send a message to the websocket server to dispatch a message to the browser, I think it would be a better implementation.

Best regards!

like image 242
nodata nodata Avatar asked Jan 13 '19 17:01

nodata nodata


People also ask

Can you use WebSockets with REST API?

REST requires a stateless protocol according to the statelessness constraint, websockets is a stateful protocol, so it is not possible.

How do you interact with WebSockets?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

What type of communication WebSocket APIs allow between clients and servers?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Should I use WebSockets or REST API?

WebSocket is ideal for a scenario where high loads are a part of the game, i.e. real-time scalable chat application, whereas REST is better fitted for occasional communication in a typical GET request scenario to call RESTful APIs.


1 Answers

If you just want to send the output of a process to a client via websockets, I would suggest you look at websocketd

You just tell it a port to listen on, and a command to run

$ websocketd --port=8080 my-program

And it will run the command when a client connects and serve them the output.

like image 78
Peter Gibson Avatar answered Oct 20 '22 19:10

Peter Gibson