How to implement socket in Anvil?

Hello Anvil community.
I’ve made a webhook in my Anvil server and want to send message from server to client everytime the webhook is called from the third party.

First I tried to implement a socket in Anvil directly, but I found that Anvil doesn’t support it. I can implement socket in server side using Anvil Uplink but I don’t know how to implement socket in client side or how to call client function from Anvil Uplink code.

Anyway, my question is how to send notification from server to client.

I’m open to any other way but don’t prefer interval function in client side.

Thanks a lot.

Welcome to the forum!

The Anvil way is to use a client side Timer to poll the server.

If you want server to client messaging then you’re looking at integrating a third-party messaging library, like Firebase. If you do some searches on the forum you’ll find people who’ve integrated Firebase and other third-party messaging systems.

1 Like

Thank you, Jshaffstall.
Honestly, I don’t like the polling way. It can cause server dump or even if we read table data from client directly I have to handle spinner and not like professional. Are there only two ways (polling or integrating Firebase)?

What is the issue with server dump that @jshaffstall’s methods would cause that a socket wouldnt?

For this, all you have to do is use a built-in context manager to not show the spinner:

    with anvil.server.no_loading_indicator:
        anvil.server.call('your_server_function')

If there’s more than 100k users and every users call server function every 10 seconds, it will give so much workload.

There are only two ways: polling, and sending data from server to client asynchronously. Firebase is one way of sending data from server to client asynchronously. There are other messaging libraries, but they all use the same basic technique. You have some Javascript code running in the client and your server sends a message to the third-party servers. That message gets relayed to the clients that have subscribed to it.

1 Like

Thank you, @jshaffstall

Hi @jshaffstall
I have a few questions.

  1. Are managing data tables on client side and calling server function and managing data table there different? I’m curious if managing data tables on client side can reduce server workload.
  2. In a case that I manage data tables on client side, it also shows spinner. How can I remove the spinner?

You remove the spinner with the code that @duncan_richards12 showed you above.

Managing tables client side can reduce some server workload, but it’s very much not secure. You can use client writeable views to mitigate that somewhat, but you’ll never get away from the fact that a sufficiently motivated hacker can use your tables in ways you don’t intend (to add credits to their account, for example, without paying money).

3 Likes

Iterator and row objects perform some magic on your behalf. For example, sometimes they immediately read all the columns of a row, or all the rows of a query, and sometimes they do it lazily, procrastinating, hoping that the stuff they don’t read now is never going to be required. When they are right, they save time, when they are wrong, they waste time. And when they waste time, if you are on the server, it’s just another very quick round trip between the web server and the database server, while if you are on the client, it’s another round trip between the client and the web/database server, which is much slower.

It is possible to fine tune that laziness, but in general dealing with the database is more efficient (and especially more secure, as @jshaffstall mentioned) when done on the server side.

1 Like

Make sense
What I consider is to reduce server workload. If I choose the way that client calls server function and server manages database, can server afford thousands of calls at a time every 10 seconds?
Maybe it can be a hundreds of thousand calls at a time repeatedly.
If yes, I’ll choose database management on server.

I can’t give a definitive answer, but I can speculate :slight_smile:

I guess that the free tier can’t manage 10,000 requests per second, and that the more you pay, they more traffic you get.

I also guess that having 100,000 web socket open would require some fine tuning on the server side. A server with a standard configuration for a typical web app likely wouldn’t handle such a massive number of concurrent connections without issues.

I would pick web sockets if real time communication was essential. And if I needed that large number of connections, I would manage my own server. But, since Anvil manages my server and my volume is much lower than that, I’m happy with having 30-40 users polling every few seconds, and with staying away from server configuration and management issues.

But I’m a low volume Anvil guy. Let’s see what more expert people have to say.

2 Likes