What do you use to create REST APIs with WebSockets?

twitter logo ・1 min read

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?

twitter logo DISCUSS (17)
markdown guide
 

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...

 

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.

 

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!

 

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...

 

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.

 

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...

 

WebSockets are made to be real-time, and bidirectional. REST is (usually) unidirectional, from the client to the server.

You better isolate on websocket only the use cases that really need those features i.e. server-initiated messages, real-time interaction, presence notification and such.

The two programming models of traditional frameworks and websocket ones (request/response vs. message passing) are different for clear and important reasons; there is little use in trying to avoid the "complications" in websocket use (i.e. everything async) because they are essential characteristics of the medium.

 

I’ve recently discovered AdonisJS. Might be worth looking at.

 

I am using Adonis for a year now side-by-side with Laravel for php projects.

 

I've also come from Laravel, the familiarity in concepts has made picking up Adonis quite smooth - and working in one language for both FE and BE is great.

 

I have read something about it but I never went deeper (I thought it was another Frontline Framework).

Thanks.

 
 
 
 

I guess you can use it for any real time service , for example getting live notifications from server

 
Classic DEV Post from Jan 16

What's your typical process for reviewing a pull request in GitHub?

I'm never sure whether I'm using this tool as effectively and efficiently as po...

David MM🐍 profile image
Software developer (Django/Vue.js). Sometimes learning, sometimes teaching what I learn at https://letslearnabout.net/

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️