Sails.js简单的私人消息(pub / sub和models)

时间:2021-04-02 23:58:12

My Goal:

我的目标:

Create a private messaging platform using Sails.js with the simplest code possible

使用Sails.js创建一个私有消息传递平台,尽可能使用最简单的代码


Assumptions of Best Practices:

最佳实践假设:

  • Use Sails.js Webockets for realtime notifications
  • 使用Sails.js Webockets进行实时通知
  • Use Sails.js PubSub for DB use with websockets
  • 使用Sails.js PubSub将数据库与websockets一起使用
  • Use Sails.js .watch() to get messages
  • 使用Sails.js .watch()来获取消息

My Questions:

我的问题:

  • Can I have a socket watch only certain new models (e.g. find where userid matches sender or recipient id) OR do I need to set up rooms? Selective watching seems much easier, but the documentation doesn't seem to support it.
  • 我可以只使用套接字监视某些新模型(例如查找userid与发件人或收件人ID匹配的位置)还是我需要设置房间?选择性观看似乎更容易,但文档似乎不支持它。
  • If any of my above assumptions or questions are not the best way to realize my goal, then what is the simplest way to implement private messaging using Sails?
  • 如果我上面的任何假设或问题不是实现我的目标的最佳方式,那么使用Sails实现私人消息传递的最简单方法是什么?

What I've Tried:

我试过的:

  • Subscribing and watching a socket
  • 订阅和观看套接字
  • Reading the Sails.js documentation
  • 阅读Sails.js文档
  • Looking at the sailsChat example (uses rooms)
  • 看着sailsChat例子(使用房间)
  • Searching * and Google for sails chat examples
  • 搜索*和Google搜索sails聊天示例

1 个解决方案

#1


6  

The simplest way I guess it is using the socket.io implemented in sails, if I remember correctly it is simply called socket.

最简单的方法我猜它是使用在sails中实现的socket.io,如果我没记错的话,它简称为socket。

All controllers can be called with socket.io (client side) IIRC. The approach that I took is to create a model called messages, and then simply create few endpoints for the messages. If you want to use the models (pub/sub) it is possible to subscribe just to the ones you want. you can subscribe every single user to just a single Model, even if you have plenty of them.

可以使用socket.io(客户端)IIRC调用所有控制器。我采用的方法是创建一个名为messages的模型,然后简单地为消息创建几个端点。如果您想使用模型(pub / sub),可以只订阅您想要的模型。您可以将每个用户订阅到一个模型,即使您有足够的用户。

What I used to do is do it manually, when I receive one message, i would emit it to the right client straight away. But if you want to write less code probably you simply have to subscribe the users to your model Model.subscribe()( http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe ) so when a message is added to the Database then you can send it to whoever you need to.

我以前做的是手动完成,当我收到一条消息时,我会立即将它发送给正确的客户端。但是如果你想编写更少的代码,你可能只需要将用户订阅到模型Model.subscribe()(http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe),这样当消息添加到数据库时,您可以将其发送给您需要的任何人。

Here's another example of a chat built on top of sails.js: https://github.com/asm-products/boxychat-old

这是另一个基于sails.js构建的聊天示例:https://github.com/asm-products/boxychat-old

#1


6  

The simplest way I guess it is using the socket.io implemented in sails, if I remember correctly it is simply called socket.

最简单的方法我猜它是使用在sails中实现的socket.io,如果我没记错的话,它简称为socket。

All controllers can be called with socket.io (client side) IIRC. The approach that I took is to create a model called messages, and then simply create few endpoints for the messages. If you want to use the models (pub/sub) it is possible to subscribe just to the ones you want. you can subscribe every single user to just a single Model, even if you have plenty of them.

可以使用socket.io(客户端)IIRC调用所有控制器。我采用的方法是创建一个名为messages的模型,然后简单地为消息创建几个端点。如果您想使用模型(pub / sub),可以只订阅您想要的模型。您可以将每个用户订阅到一个模型,即使您有足够的用户。

What I used to do is do it manually, when I receive one message, i would emit it to the right client straight away. But if you want to write less code probably you simply have to subscribe the users to your model Model.subscribe()( http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe ) so when a message is added to the Database then you can send it to whoever you need to.

我以前做的是手动完成,当我收到一条消息时,我会立即将它发送给正确的客户端。但是如果你想编写更少的代码,你可能只需要将用户订阅到模型Model.subscribe()(http://sailsjs.org/documentation/reference/web-sockets/resourceful-pub-sub/subscribe),这样当消息添加到数据库时,您可以将其发送给您需要的任何人。

Here's another example of a chat built on top of sails.js: https://github.com/asm-products/boxychat-old

这是另一个基于sails.js构建的聊天示例:https://github.com/asm-products/boxychat-old