Adapter
As mentioned in the beginning of this chapter, Nest.js comes with it own adapter, which uses socket.io
. But the framework needs to be flexible and it can be used with any third party library. In order to provide a way to implement another library, you have the possibility to create your own adapter.
The adapter has to implement the WebSocketAdapter
interface in order to implement the following methods. For example, we will use ws
as a socket library in our new adapter. To use it, we will have to inject the app
into the constructor as follows:
export
class
WsAdapter
implements
WebSocketAdapter
{
constructor
(
private
app
:INestApplication
)
{
}
/* ... */
}
By doing this, we can get the httpServer
in order to use it with the ws
.
After that, we have to implement the create
method in order to create the socket server.
export
class
WsAdapter
implements
WebSocketAdapter
{
/* ... */
create
(
port
:number
)
{
return
new
WebSocket
.
Server
({
server
:this...