Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface WebSocketControllerConfig<RequestType, ResponseType, UnderlyingDataType>

This interface describes websocket behaviour, serialization, deserialization, authorization, subscriptions etc. The auto reconnect settings are passed to the WebSocketController.open method.

It is supposed that you usually communicate with a server using request-response pattern. So every message being sent have an id and corresponding response have the exact same id. To delegate the control over id's, you pass WebSocketControllerConfig.setRequestId function, which transforms your message, so it has the id set, and you pass WebSocketControllerConfig.getResponseId function, to get response id. If you do not want to set id on a request message, you can use options param of the WebSocketController.send method.

The id must be number. If you need to use a string or something else, transform this number to the string in the set function, and back to the number in the get function.

Usage

TODO: how to use.

Type Parameters

  • RequestType

    The type of messages, that will be consumed by WebSocketController.send and WebSocketController.request methods.

  • ResponseType

    The type, of messages, that will be produced by WebSocketController.messages$ observable.

  • UnderlyingDataType extends string | ArrayBufferLike | Blob | ArrayBufferView = string

    Must be ArrayBuffer, Blob or string. The result of the serializer function and a parameter of the deserializer. It is supposed that you send only messages of this type through the underlying WebSocket. DO NOT COMBINE "string", "arraybuffer" and "blob": it is not supported. (even that original WebSocket supports). If you send binary data, send ONLY binary data. If you send text frames, send ONLY text frames.

Hierarchy

  • WebSocketControllerConfig

Index

Properties

WebSocketCtor?: (new (url: string | URL, protocols?: string | string[]) => WebSocket)

Type declaration

    • new (url: string | URL, protocols?: string | string[]): WebSocket
    • A WebSocket constructor to use.

      Parameters

      • url: string | URL
      • Optional protocols: string | string[]

      Returns WebSocket

authorize?: { createRequest: any; isResponseSuccessful?: any }

If passed, an authorization message will be sent whenever the socket opens. When not passed, socket state transition opened->authorized still happens, but instant.

Authorization request MUST have a response.

Type declaration

binaryType?: "blob" | "arraybuffer"

Sets the binaryType property of the underlying WebSocket. if UnderlyingDataType type param is set to be string, and you communicate with server through text frames, this parameter does not matter. If UnderlyingDataType type param is set to be Blob , binaryType must be 'blob'. If set to be ArrayBuffer, binaryType must be 'arraybuffer'

buffer?: { overflow: BufferOverflowStrategy; size?: number }

Helps to preserve messages, when reconnection happens. When the message comes, but the socket is not ready to consume it, the message goes to the buffer and being sent later, when the socket is ready. The buffer is enabled by default. If the message was dropped from the buffer (because of the overflow), the observable returned by the WebSocketController.request function throws RxJs TimeoutError Note, there are different buffers for authorized messages, subscribed messages, and for messages that don't require eiter.

Type declaration

protocol?: string | string[]

The protocol to use to connect, passed directly to a WebSocket constructor

requestTimeout?: number

If there is no response during requestTimeout after sending the request, the rxjs TimeoutError is thrown into observable.

subscribe?: { createRequests: any; isResponseSuccessful?: any }

This param is used when your socket starts sending updates of some data only after you ask it for. To tell server, that you need regular updates, you send a subscribe request. To tell that you don't, you send unsubscribe request. Updates will be handled as ordinary messages - passed to the WebSocketController.messages$ observable.

When not passed, socket state transition authorized->subscribed still happens, but instant. Subscription requests MUST have a response.

Type declaration

  • createRequests:function
    • createRequests(authResponse?: ResponseType): Observable<undefined | RequestType[]>
    • Creates a request array, after sending every request of which the socket will be subscribed. Array of responses to these requests is emitted into the WebSocketController.subscribed$ observable and every response from array is emitted into WebSocketController.messages$ observables one by one.

      Parameters

      • Optional authResponse: ResponseType

        A response on the message, created by the authorize.createRequest. If no authorizeIsResponseSuccessful was provided, authResponse will be null.

        if no value is returned, the socket is closed and not reopened.

      Returns Observable<undefined | RequestType[]>

  • isResponseSuccessful?:function
    • isResponseSuccessful(response: ResponseType): boolean
url: string | URL

The url of the socket server to connect to, passed directly to a WebSocket constructor

Methods

  • deserializer(responseEvent: MessageEvent<UnderlyingDataType>): ResponseType
  • Function to be called on every message, received by "onmessage" handler of underlying socket. If not stated, ResponseType must be equal to the UnderlyingDataType. If you want to communicate through JSON, pass JSON.parse method here.

    Parameters

    • responseEvent: MessageEvent<UnderlyingDataType>

      The whole event, in case you need not only data.

    Returns ResponseType

  • getResponseId(response: ResponseType): number
  • isErrorResponse(response: ResponseType): boolean
  • serializer(request: RequestType): UnderlyingDataType
  • setRequestId(request: RequestType, id: number): RequestType

Generated using TypeDoc