如何在Meteor中从服务器向客户端发送消息?

时间:2021-12-15 16:45:46

I want to send a message from a Server script in 'server/main.js' to a Cliente script in 'client/main.js'.

我想从'server / main.js'中的服务器脚本向'client / main.js'中的Cliente脚本发送消息。

I tried a few things with Publish/Subscribe, but i must have done something wrong. The console where the meteor server is running gone crazy with thousands of error messages.

我尝试了一些发布/订阅的东西,但我一定做错了。流星服务器运行的控制台发疯了数以千计的错误消息。

server

Meteor.publish("test", function () {
    this.ready();
    return 'some test';
  });

client

Template.panel.onCreated(function loginOnCreated() {
  Tracker.autorun(function () {
    const handle = Meteor.subscribe('test');
    if(handle.ready()){
      alert('Done')
    }
  });
});

Anyway, I need a server method to call something in client when it's ready.

无论如何,我需要一个服务器方法在客户端准备就绪时调用它。

Reason: Template.panel.onCreated can't query data from Mongodb. It has to wait some seconds. So what I want to do is not possible in Template.panel.onCreated in client. It has to wait until Mongo is ready.

原因:Template.panel.onCreated无法从Mongodb查询数据。它必须等待几秒钟。所以我想做的事情是在客户端的Template.panel.onCreated中无法做到的。它必须等到Mongo准备好了。

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


1  

You should put Meteor.subscribe outside of Tracker.autorun.

你应该将Meteor.subscribe放在Tracker.autorun之外。

I think the problem is because handle is a reactive data source, so when it changes the function inside Tracker.autorun re-run, it subscribe to server again, a new handle is created and the new handle cause the function to run again. This loops over and over and throw many error messages to your console

我认为问题是因为handle是一个被动数据源,所以当它改变Tracker.autorun重新运行的内部函数时,它再次订阅服务器,创建一个新句柄,新句柄使该函数再次运行。这会一遍又一遍地循环并向控制台发送许多错误消息

#2


1  

It has to wait some seconds.

它必须等待几秒钟。

I am assuming data you are retrieving from Mongo is necessary for dom population. For any waiting process you can use loader. You can do this by setting a helper which will be 'false' till you don't get response from server. When you do get that response, populate dom and set this field to true.

我假设您从Mongo检索的数据对于dom人口是必要的。对于任何等待过程,您可以使用加载器。您可以通过设置一个“false”的帮助程序来完成此操作,直到您没有从服务器获得响应。当您获得该响应时,填充dom并将此字段设置为true。

Something like:

{{#if dataLoaded}}
  {{domYouWantToPopulate}}
{{else}}
  {{spinner}} //your loading icon template
{{/if}}

Or even if you don't need to data for dom still loading icon for stop user do something till data is received.

或者即使您不需要dom的数据仍然加载图标停止用户做一些事情直到收到数据。

So see the thing is, assuming you are in development, you have only client but in real scenario you'll have one server and many clients. So calling client function on every client from server is wrong from both processing and feasibility metrics. Thats why we have APIs to get response from server, so server can send response to only that client which have hit that API.

所以看到的是,假设您正在开发中,您只有客户端,但在实际情况下,您将拥有一台服务器和许多客户端。因此,从服务器调用每个客户端上的客户端功能都是错误的处理和可行性指标。这就是为什么我们有API来从服务器获得响应,因此服务器只能向那个已经命中该API的客户端发送响应。

#3


0  

You can do it by updating the database and the client helper method alerts it to the fact that the server wanted to say something, or you can use a package like this:

您可以通过更新数据库来执行此操作,并且客户端帮助程序方法会警告服务器想要说些什么,或者您可以使用这样的包:

https://atmospherejs.com/raix/eventddp

It allows server initiated messaging. I haven't tried it, but it comes from a good source

它允许服务器启动消息。我没有尝试过,但它来自一个很好的来源

#1


1  

You should put Meteor.subscribe outside of Tracker.autorun.

你应该将Meteor.subscribe放在Tracker.autorun之外。

I think the problem is because handle is a reactive data source, so when it changes the function inside Tracker.autorun re-run, it subscribe to server again, a new handle is created and the new handle cause the function to run again. This loops over and over and throw many error messages to your console

我认为问题是因为handle是一个被动数据源,所以当它改变Tracker.autorun重新运行的内部函数时,它再次订阅服务器,创建一个新句柄,新句柄使该函数再次运行。这会一遍又一遍地循环并向控制台发送许多错误消息

#2


1  

It has to wait some seconds.

它必须等待几秒钟。

I am assuming data you are retrieving from Mongo is necessary for dom population. For any waiting process you can use loader. You can do this by setting a helper which will be 'false' till you don't get response from server. When you do get that response, populate dom and set this field to true.

我假设您从Mongo检索的数据对于dom人口是必要的。对于任何等待过程,您可以使用加载器。您可以通过设置一个“false”的帮助程序来完成此操作,直到您没有从服务器获得响应。当您获得该响应时,填充dom并将此字段设置为true。

Something like:

{{#if dataLoaded}}
  {{domYouWantToPopulate}}
{{else}}
  {{spinner}} //your loading icon template
{{/if}}

Or even if you don't need to data for dom still loading icon for stop user do something till data is received.

或者即使您不需要dom的数据仍然加载图标停止用户做一些事情直到收到数据。

So see the thing is, assuming you are in development, you have only client but in real scenario you'll have one server and many clients. So calling client function on every client from server is wrong from both processing and feasibility metrics. Thats why we have APIs to get response from server, so server can send response to only that client which have hit that API.

所以看到的是,假设您正在开发中,您只有客户端,但在实际情况下,您将拥有一台服务器和许多客户端。因此,从服务器调用每个客户端上的客户端功能都是错误的处理和可行性指标。这就是为什么我们有API来从服务器获得响应,因此服务器只能向那个已经命中该API的客户端发送响应。

#3


0  

You can do it by updating the database and the client helper method alerts it to the fact that the server wanted to say something, or you can use a package like this:

您可以通过更新数据库来执行此操作,并且客户端帮助程序方法会警告服务器想要说些什么,或者您可以使用这样的包:

https://atmospherejs.com/raix/eventddp

It allows server initiated messaging. I haven't tried it, but it comes from a good source

它允许服务器启动消息。我没有尝试过,但它来自一个很好的来源