Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

62
Views
How to use the WebSocket readyState to display connection status in app header

I am creating a chat widget using svelte. I want to display the websocket is connected or not using the websocket.readyState property, the readyState values as follows

  • 0- Connecting,
  • 1- Open,
  • 2- Closing,
  • 3- Closed

the status i want to display in app header is

  • connecting while state is 0
  • online when state is 1
  • disconnected when state is 2
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Well according this documentation you can use the events to know. MDN

0   CONNECTING  Socket has been created. The connection is not yet open.
1   OPEN    The connection is open and ready to communicate.
2   CLOSING The connection is in the process of closing.
3   CLOSED  The connection is closed or couldn't be opened.
  • so when socket = new WebSocket(url) status is CONNECTING
  • when onopen = (event) => { } status is OPEN
  • when onclose or onerror status is CLOSED
  • when invoking socket.close() status is CLOSING

It might be some other situation for CLOSING but not something I know of.

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!