SocketCluster + LiveQuery
Hi , Currently i am working on adding real time chat capabilities to my app. The requirement is that users will be able to chat with each other in real time and also see the chat history. At the beginning i was thinking about Firebase which provide "real time database" that do exactly this but i have 2 issues with it:
- It require Firebase user (because i really don't want to use anonymous)
- It's stored on a different DB which not under my control and in the future it can make life really hard. (e.g. changing tech stack, replication etc.)
Since i am a big fan of parse-server and NodeJS technologies I was looking for some NodeJS real time messaging server and then i found SocketCluster which seems to be very powerful especially for solutions which require high performance & easy scale
My current solution is based on LiveQuery which is very close to what i want because you can implement "Real time database" capabilities easily with LiveQuery since all events are emitted after one or more objects were saved in the database. The problem is the other part of Real time messaging: User Presence (when user is online/offline), user is typing , message was read by the user etc. I tried to implement all of that with LiveQuery but it is not simple at all and require a lot of DB work in order to make this work (since LiveQuery raise the events only after objects has been saved)
So I was thinking maybe we can leverage SocketCluster capabilities and use them in parse-server and maybe (but only maybe) SocketCluster can replace the WebSocket layer of Parse LiveQuery which currently use only the official WebSocket npm module under the hood.
SocketCluster uses uws instead of ws. What would be the benefit of depending upon SocketCluster?
Hi @flovilmart
uws is only one of the advantage (BTW. you can use also ws there if you want..)
Other advantages are:
From their website
- Vertical scaling (more CPU cores and more workers) and Horizontal scaling (more hosts)
- Support both pub/sub channel and direct communications
- Sockets automatically reconnect in case of failure
- Optimized to run and autoscale on Kubernetes (Good for us and to all other users who use k8s)
- High Performance (http://socketcluster.io/#!/performance)
you can find the list of features in here From what i have tested
- Client libraries for most of the languages (both client and server technologies)
- Easy to use and integrate
- Allow users to subscribe and publish to channels (without being dependant on Database commit) so it's very good for real time scenarios
- I managed to deploy it to k8s and run it in HTTPS very fast ( if we will decide to use it we can provide a user guide for parse-server users)
What i am thinking is one of the following:
- Replace the current websocket layer of LiveQuery to use SocketCluster
- Create adapter that will allow users to use SocketCluster instead of the current solution
- Integrate it in cloud code (afterSave and afterDelete) so each time object is being saved or deleted we can show how to emit events to SocketCluster server.
I'm not a big fan of introducing a major dependency like that project into parse-server, because that forces to keep up with upstream changes as well as fully supporting the upstream project. However, I believe there is a possible middle ground that's possible, after all you can probably replace the PubSubAdapters for publish / subscribe with your own implementation based on SocketCluster no?
Actually the Pub/Sub adapter should runs on top of SocketCluster. SocketCluster should replace the WebSockets implementation and not the Pub/Sub adapter. In our case (parse-server) SocketCluster should replace the LiveQueryServer. BTW SocketCluster also introduce Pub/Sub integration (Redis, RabbitMQ). What i was thinking is maybe not to replace the ws layer of parse-server but create WebSocketAdapter that will allow users to replace the core of the implementation with SocketCluster.
Another option can be to not use LiveQuery at all and to emit events to the SocketCluster server from cloud code using SocketCluster client libraries for NodeJS.
BTW! I am also not a big fan of introducing dependencies and that's the reason why i think that the integration should be done via external module and not as internal module in parse-server. Also i think that all the auth adapters (e.g. google, github, facebook, twitter etc.) should be added as external modules but this is related to another thread.
Also i think that all the auth adapters
We encourage additional auth adapters to be added externally.
Another option can be to not use LiveQuery at all and to emit events to the SocketCluster server from cloud code using SocketCluster client libraries for NodeJS.
That would work perfectly, this is actually what I would recommend. Maybe there is some refactor that can be done in order to swap part of the live query implementation, and keep the buisiness logic in parse-server (determine wether or not a trigger should be send).
Remember also that there are other platforms that 'need' the liveQuery client.
Not sure how much faster is SocketCluster, if we use uws instead of ws this seems to me that the main benefit would be the automatic k8s deployment. (which is also something we can work on for parse-server with proper configuration generation)
Agree. But think about it that with the current solution you must save something into the DB in order to receive the events but what is missing is the ability to send events from client to one or more clients without being depended on the DB layer. I found myself looking for cloud services like PubNub or Pusher (which are quite expensive..) and then i was thinking why not use an open source solution and then i found SocketCluster, tested it and really liked it and the next thing that i was thinking is how we can bring it to the parse-server platform. I think that it can be great to have it.
I think that at the first phase we can start with documentation or blog on how it can be done only via cloud code events (without really changing parse-server core) and then we can think about how we can take it to the next stage.
BTW. You can see here a benchmarks table about uws and it looks like uws is more lightweight with high performance especially for high throughput .
As you can see I added uWS as an optional dependency, so everyone benefits the performance upgrade.
you must save something into the DB in order to receive the events
Yeah, that's the whole point of parse-server. I'm not sure, if we start adding more feature that it would not be a dis-advantage.
In that sense, parse-server integrates very well through cloud code, and express, I don't believe the open source project should be doing everything. What you're describing is a very specific use case, with a project that's sole dedication is to run on k8s scalable WS. parse-server's mission is not about scalable websockets but a secure, simple to use, backend as a service that abstracts the need to use a custom ORM.
I believe both can live together but that bending parse-server to accommodate SocketCluster's architecture is not something I want to pursue.
What I did was use firebase but with parse user system. Make the login procedure through cloud code. In the cloud code you can use firebase auth system to generate a token for the user's object id. And return the session code for both parse and firebase. In this way we can use the real-time capabilities of firebase with security and also have a single auth system
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.