3 ms·
While adding chat to a site, I realized the same conclusions. I started adding browser->server commands to my WebSocket instance, but then I realized I had eve
by munro 12y ago
While adding chat to a site, I realized the same conclusions. I started adding browser->server commands to my WebSocket instance, but then I realized I had everything already setup to do that in my API, and I was just splaying browser->server logic over two transports.
WebSockets definitely complicate the code, since you now have to deal with the connection before you can send a request. Keeping it constrained to realtime events cleaned things up nicely.
I was also trying to build some persistence into replaying chat messages on reconnection (data sync), but it was way easier to just turn "get_last_messages" into an endpoint that gets called on page load, or on socket reconnection.
- pedalpete 12y agoIf I understand your first problem correctly, you could have setup an RPC system so you only write your apis once. Use the same 'endpoint' of your api as the data call in your socket connection, and you only end up writing your api once.
- munro 12y agoI can definitely hit the API from the websocket server, but I just didn't even bother! :D I just wrote the websocket server to do publishing events, and left syncing issues to the API