DEV Community

loading...

What do you use to create REST APIs with WebSockets?

davidmm1707 profile image David MM🐍 ・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?

Discussion (18)

pic
Editor guide
Collapse
yaser profile image
Yaser Al-Najjar

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
diegomgar profile image
Dieg Oto

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🐍 Author

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
michelemauro profile image
michelemauro

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.

Collapse
jamesthomson profile image
James Thomson

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

Collapse
davidmm1707 profile image
David MM🐍 Author

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

Thanks.

Collapse
wobsoriano profile image
Robert

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

Collapse
jamesthomson profile image
James Thomson

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.

Collapse
themisir profile image
Misir Jafarov

ws.js. Socket.io is very slow.

Collapse
oxygen profile image
oxygen

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
attacomsian profile image
Atta

Check out Socket.io. You can use it in Node.js/Express.

Collapse
themisir profile image
Misir Jafarov

Socket.io is very slow

Collapse
tomerl101 profile image
Tomer

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

Collapse
jobm profile image
Collapse
bonanhel profile image
bonanhel

Give a try to nginx nchan :)