Mixing Websites and REST

I am writing a RESTfull api where, for example, a user can create new threads or new messages in a thread. Here I will use a regular POST request. My API also allows users to send messages to each other. And when the user receives the message, I want to receive notifications in the browser, this is where I will use websockets instead of polling every few seconds.

My questions

  • Since I already have an open connection with websocket where messages will appear, should I use this connection to send messages, create new threads or messages?

  • Can I use a POST request to create messages and web maps to receive them in real time and a GET request to get message history? Is this a good practice?

  • I use the django rest framework, which handles field validation for me, as if I would handle validation if I create a resource using websocket instead of the usual POST request.

I'm starting to develop a RESTfull API, and I just started developing it with websockets. Sorry for any stupid questsions that might seem so logical to you :)

thank

+4
source share
2 answers

Since I already have an open connection with websocket, where messages will come, should I also use this connection to send messages, create new topics or messages?

. HTTP POST, CQRS, , ; , WebSockets.

POST - GET-, ? ?

. REST WebSocket . WebSockets REST API. , , "", , , , , .

django rest framework, , , , websocket POST.

django, , - .

0

3- ,

def ws_create_item(message):
    data = json.loads(message['text'])
    serializer = ItemSerializer(data=data)
    if serializer.is_valid():
        serializer.save()

ItemSerializer DRF, ws_create_item WebSocket-.

0

Source: https://habr.com/ru/post/1668701/


All Articles