Noob: How do I use a variable from client main.js on server main.js?


#1

Hi!
Let say I have a variable named X on the client side, and then I want to set X = 5 using the server.

How do I do that? (it seems like it has changed in 1.3 where all the old tutorials have server and client in the same file)


#2

You could for example use a meteor method as described here https://guide.meteor.com/methods.html. You would call it from the client and the server would return the value 5.

You could also make a publication on the server that you subscribed to on the client. It is described here https://guide.meteor.com/data-loading.html#publications-and-subscriptions. Then the value on the client would change every time you changed it on the server.


#3

As @birk already has written - server methods or publication (or streams) - will transfer values from client to server back and for.

It doesn’t depend or work on same files for client and server. Remember that files for client and server are packaged during build and then published to client on connect or saved to server directory. So if you wrote X=5 even if in a section for server, that var value will never be shared between client and server. Totally separated processes.

Cheers
Tom


#4

Thanks @birk and @tomfreudenberg! It seemed so advanced reading the guide, but I found this video https://www.youtube.com/watch?v=0YxRYAGO5QY

I just made extremely complicated for myself, thinking it could not be so easy. Thanks for the replies :smiley: