通过Websocket与NSQ进行通信

huangapple go评论78阅读模式
英文:

Communicate with NSQ over a Websocket

问题

在用户的浏览器中运行的Web应用程序是否可以通过WebSocket连接与NSQ进行通信?

任何内置的NSQ功能或第三方库的建议都将非常有帮助。

英文:

Is it possible to communicate with NSQ over a websocket connection from a web app running in a user's browser?

Any built-in NSQ features or third-party library suggestions would be of a great help.

答案1

得分: 1

最近有一个名为WSQ的库可以解决通过WebSocket连接与NSQ进行通信的任务。

它允许在WebSocket和NSQ两端分别定义消息的编码/解码逻辑。此外,WSQ支持注入身份验证和授权逻辑,控制用户对订阅和发布主题的访问,并根据消息内容对传入消息进行过滤。

创建WSQ服务器示例:

// 创建默认配置
wsqConfig := wsq.NewConfig()
// 配置WebSocket CheckOrigin回调以绕过任何检查。
// 在生产环境中不要这样做!
wsqConfig.SetWSCheckOrigin(wsq.CheckOriginBypass)

// 创建服务器实例,指定要使用的消息和用户类型。
server := wsq.NewServer[message, *wsq.AnonymousUser](
	// 要监听的地址(可选)和端口
	":9980",
	// WSQ配置实例
	wsqConfig,
	// NSQ配置实例
	nsq.NewConfig(),
	// WSQ Transformer结构提供NSQ和WebSocket两端的编码器/解码器
	&wsq.Transformer[message]{NSQEnDec: &nsqEnDec{}, WSEnDec: &wsEnDec{}},
	// 身份验证控制器
	&wsq.NoAuthentication,
)
server.Run()
英文:

As of recently there is the WSQ library that solves the task of communicating to NSQ over the websocket connection.

It allows to define message encoding/decoding logic on Websocket and NSQ sides separately. Also, WSQ supports injecting authentication and authorization logic, controlling user's access to subscribing and publishing to topics, and incoming message filtering based on message's content.

Creating WSQ server example:

// Create the default config
wsqConfig := wsq.NewConfig()
// Configure Websocket CheckOrigin callback to bypass any checks.
// Don't do it in production!
wsqConfig.SetWSCheckOrigin(wsq.CheckOriginBypass)

// Create the server instance specifying message and user types to use.
server := wsq.NewServer[message, *wsq.AnonymousUser](
	// Address (optional) and port to listen on
	":9980",
	// WSQ Config instance
	wsqConfig,
	// NSQ Config instance
	nsq.NewConfig(),
	// WSQ Transformer struct providing encoders/decoders for NSQ and Websocket sides respectivly
	&wsq.Transformer[message]{NSQEnDec: &nsqEnDec{}, WSEnDec: &wsEnDec{}},
	// Authentication controller
	&wsq.NoAuthentication,
)
server.Run()

huangapple
  • 本文由 发表于 2022年7月26日 05:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/73115743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定