moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

WebSockets Transport Layer

Open Wallacy opened this issue 6 years ago • 15 comments

Im working on a WebSockets Transport Layer (just started) for Moleculer, and i'm open this issue to discuss.

I don't know if anyone has this need to, but in may case, i need to push a real-time data to the end user.

The idea is not make a complete pub/sub, wrapper like socket.io, but a complement to API Gateway (moleculer-web) at some point.

First i'm planing to make a regular WebSockets transport for the server side, after that work go to the client side, maybe implementing a lite version of the moleculer broker to be used from the browser.

In the future im planning adding a WebRTC/SCTP transport too. (just a quick reference for why)

Wallacy avatar May 07 '18 16:05 Wallacy

Good idea. @ColonelBundy is working on a similar project: https://github.com/ColonelBundy/moleculer-ws

icebob avatar May 07 '18 16:05 icebob

Nice, i missed this project. So, i'm not alone xD

BTW: Will be nicer to call moleculer-web as moleculer-gateway, and then make the "rest" handler moleculer-rest, and plug into gateway? In this way we can easily insert a molecular-ws, molecular-soap or other client side communication mechanism.

Wallacy avatar May 07 '18 17:05 Wallacy

I've found that moleculer runs very well with Primus (with uws under the hood); and I've been working on a cluster-compatible version based on Socketcluster which also has been reliable so far. In terms of using this as a transporter internally, would it really lead to a noticeable difference in speed for the end-user, vs NATS or the other transporters currently available ?

kevinsegal avatar May 09 '18 23:05 kevinsegal

I did not finish yet. I’m also working on service discovery for the websocket, but my early tests is very close to the TCP implementation, so will be fast than NATS.

For now, im only trying to create a broken that at some point will be able to communicate directly to the browser without a 3rd party project like Primus.

After that a will put on the table some things that can make a difference for some people. (Service Discovery is already one)

Wallacy avatar May 10 '18 00:05 Wallacy

Any updates about it? Are you working on it yet?

icebob avatar Jun 10 '18 08:06 icebob

Hello,

I did a simple version, but i got a lot of work last 3 weeks and did not have a change to finish. Sorry about the delay, i plan to rollout next week. Sorry about that.

Wallacy avatar Jun 10 '18 19:06 Wallacy

Hello,

Probably my week is measured on mars or jupiter calendar! I will try again this week! ;)

Wallacy avatar Jul 08 '18 14:07 Wallacy

Any news? :)

icebob avatar Jul 25 '18 19:07 icebob

Sorry, i need more time. Too busy! But you can close the issue if want. When i finish i will make the P.R in any case.

;)

Wallacy avatar Jul 27 '18 14:07 Wallacy

Finally i get the time to do this!

But a have a few doubts about the best approach for the browser side, is better adapte moleculer to be compatible with browserify (using ws/json/protobuf only) or make another project to handle this? The best part of moleculer is the lvl4 load balancing and replicate this and keep in sync with the project evolution will be painfull.

@icebob Do you have any restriction to adapt moleculer to support browserify as a subtask of this one?

Wallacy avatar Aug 08 '18 13:08 Wallacy

I don't know browserify, so I don't know, how we can adapt moleculer to the browser side. I think it's better to make a browser WS client which can communicate with a WebSocket Gateway service.

icebob avatar Aug 11 '18 18:08 icebob

The problem with that is there's no gain from current exemple with socket.io talking with the gateway.

https://github.com/moleculerjs/moleculer-web/blob/master/examples/socket.io/index.js

You can do the same in any broker. Without a more complete approach is hard to justify any change.

I tried to compile with browserify but to make that work correctly i needed make a big change and the performance was not good.

So i will try a midle groud here, full browserify will not work then I will try some config to import only the code needed to get the moleculer lvl4 load balancer work at browser side.

I need more coffe xD

Wallacy avatar Aug 14 '18 02:08 Wallacy

By the way, I've created a WS transporter for the fullstack-moleculer project: https://github.com/icebob/fullstack-moleculer/blob/master/backend/WebsocketServerTransporter.js https://github.com/icebob/fullstack-moleculer/blob/master/frontend/src/WebsocketClientTransporter.js

icebob avatar Apr 08 '21 13:04 icebob

Nice,

The only think that i dislike on that is the use of socket.io! The performance impact is very high. The traditional ws (or uws) apear to be a better fit.

I was not aware of the fullstack-moleculer project. I did something similar using typescript+react; I create a useMachine hook to make "calls" at moleculer backend (using websocket) and then mapped all functions/services (by name) using typescript to use on that hook.

The result was pretty great, i ditch all rest api of our services, and the service is in productions for a year. i'm actually recommending moleculerjs to everyone in several full stack applications. If the person is good at typescript can be much more productive than traditional REST API/GraphQL;

Thanks for the project icebob!

Wallacy avatar Apr 08 '21 14:04 Wallacy

Thanks for your recommendation. I'm glad that you are satisfied. The fullstack-moleculer just a PoC, I don't recommend it in production, and it can works only with a small user count because every browser is a Moleculer node, and if there are too many nodes, it will cut down the performance of communication.

Yeah, I know that socket.io is slow but easier to implement and in this project, the development speed was more important so that I can check the concept. I've just linked the files that can be a starting point for a real WS transporter.

icebob avatar Apr 08 '21 15:04 icebob

Because NATS server can run a WebSocket server out of the box, implementing a specialized transport will be redundant. In addition, there are already several modules for WebSocket under Moleculer. Therefore, I propose to close this issue as obsolete.

intech avatar Oct 10 '22 20:10 intech