Gateways
The gateways in the class using the decorator previously seen contain all of the handlers that you need to provide the results of an event.
Nest.js comes with a decorator that allows you to access the server instance @WebSocketServer
. You have to use it on a property of your class.
export
class
CommentGateway
{
@
WebSocketServer
()
server
;
/* ... */
}
Also, throughout the gateway, you have access to the injection of injectable services. So, in order to have access of the comment data, inject the CommentService
exported by the CommentModule
, which has been injected into this module.
export
class
CommentGateway
{
/* ... */
constructor
(
private
readonly
commentService
:CommentService
)
{
}
/* ... */
}
The comment service allows you to return the appropriate result for the next handlers.
export
class
CommentGateway
{
/* ... */
@
SubscribeMessage
(
'indexComment'
)
async
index
(
client
,
data
)
:
Promise
<
WsResponse
<
any
>>
{
...