CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use with WebSockets

matiasmarcodelpont opened this issue · comments

My http client authenticates with the server using a bearer token sent on an Authentication header. I have implemented an interceptor and a retry policy to handle the logic when the token must be refreshed.

Now I want to use te same InterceptedClient to create a WebSocket with the same server. The request uses the same Authentication header and must be updated in the same manner.

Is this possible?

I don't think it is possible to do so since HTTP (https://) and WebSocket (ws://) are two different protocols.

WebSocket is a framed and bidirectional protocol. On the contrary, to this, HTTP is a unidirectional protocol functioning above the TCP protocol.

  • WebSocket protocol is capable to support continual data transmission, it’s majorly used in real-time application development.
  • HTTP is stateless and is used for the development of RESTful and SOAP applications.

That said, this package is built on top of http which was not thought for handling websockets. I think your best approach might be to abstract the behavior for token usage to a separate class and use/reuse the same instance in both http and websocket calls or try to find an alternative and more robust package that handles both scenarios (sad but true 😞 )