DEV Community

What do you use to create REST APIs with WebSockets?

When creating REST APIs I use the Django Rest Framework, as I love Django and Python.

But when it comes to WebSockets, it feels too complicated to what it should be an easier task.

What language and framework do you love to use to create REST APIs with WebSockets?

Top comments (16)

Collapse
 
yaser profile image
Yaser Al-Najjar • • Edited

I tend to avoid websockets all together.

Why?

They're unreliable & hard to scale, the connection might drop, and you might find yourself doing polling for the sake of reliability...

Before going websockets, I would say please read this:

blogs.windows.com/windowsdeveloper...

Collapse
 
diek profile image
diek •

As you say, websockets and unreliable and hard to scale, very much... but they are very useful and helps to make tools that enhance the UX.
Answering to @DavidMM, i must say that websockets and an api rest are not compatibles in the same scenario, api rest are meant to be an question & answer architecture. If you want to have websockets, you need to have a server with a software that allows them, Node is an example.
Anyway, you can have an api rest working on some machine, and a server side that gives you the websocket feature for your needs, they can be on different machines, same machine and different software or the same software. For example, i have made an api rest in node+express that at the same times allows websockets that sends and receives messages.

Collapse
 
mostafaahamidmanon profile image
Mostafa A. Hamid •

Maybe he cannot study it... :)

Collapse
 
krusenas profile image
Karolis •

If you are familiar with Go, then this package is really great - github.com/gobwas/ws :)

On the backend side I usually connect websocket to a NATS channel for that customer's resource, this way it's easy to scale websocket clients horizontally without much effort. WebSockets are great for UI updates, a lot better than polling.

Check out SSE as well: developer.mozilla.org/en-US/docs/W...

Collapse
 
davidmm1707 profile image
David MMπŸ‘¨πŸ»β€πŸ’» •

I work with Python and Javascript, so I wanted to learn a backend language strongly typed.

And everything points that I should learn Go. Thanks for the link and for pushing me a step more towards Go.

Collapse
 
jackmichaud profile image
Jack Michaud •

I tended to err away from WebSockets until I tried making a socket server with AWS API Gateways.

A high level overview: a WebSocket connection triggers a lambda where you log a connection ID into a database. The connection ID can be used to send data back to that connection through other lambdas. The disconnecting the WebSocket triggers another lambda where you can remove the connection ID. aws.amazon.com/blogs/compute/annou...

I like this solution because it's highly scalable and fits into my work's infrastructure well. I know AWS isn't for everyone!

Collapse
 
oxygen profile image
oxygen • • Edited

All APIs are fundamentally RPC. For example, if you would use JSON-RPC over HTTP it could still be considered REST as long as you follow REST principles (the JSON-RPC would be just be a standard way to represent all requests and all responses).

Here’s a library that allows you to export APIs over both HTTP and WebSocket on the same server

github.com/bigstepinc/jsonrpc-bidi...

Collapse