Socket
Socket
Sign inDemoInstall

socketconnect

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    socketconnect

WebSocket wrapper with HTTP repeat request fallback, providing a shared API for sending and receiving data.


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

SocketConnect

Incredibly simple and extendable WebSocket wrapper with an HTTP repeat request fallback providing a shared API for sending and receiving data. SocketConnect checks if WebSockets are available to the client, if so they perform as normal, if not it falls back to repeat HTTP requests. You can then compare responses and check for new data.

Documentation

Import or load the module

var SocketConnect = require('socketconnect');

import SocketConnect from 'socketconnect';

Create a new instance

var connection = new SocketConnect(websocketURL, httpFallbackURL);

If your websocketURL works as a HTTP fallback by replacing ws:// with http:// there's no need for the second argument as SocketConnect will perform the replacement. However if that's not the case, the second argument is necessary for the HTTP fallback.

WebSocket / HTTP Shared API

Sending messages

connection.send(data);

Handling data
connection.handleData = function(e) {
  console.log('Data:', e);
};
Set up custom handling environment

There are two flags for checking whether your SocketConnect has used websockets or the fallback. Use some logic like below to separate your custom event handling:

if (connection.isWebSocket) {
  // Custom WebSocket handling here
} else if (connection.isHTTP) {
  // Custom HTTP handling here
}

The below API is split is split into WS/HTTP, make sure you override them within the correct block of the above logic.

WebSocket

Event handling

You'll want to override the native WebSocket event handlers, you can do this on connection.ws.*event*, e.g.:

connection.ws.onopen = function() {
  console.info('Connected!');
};

The same applies for onerror, onclose, however you'll handle onmessage within the shared API handleData.

IMPORTANT: You must set connection.canSend = true; within the onopen event, this ensures you cannot send messages before the connection is established.

HTTP

SocketConnect falls back to HTTP repeat requests using XMLHttpRequest.

Setting request interval

The request interval is set to 1000(ms) by default, but you can change that using:

connection.setReqInterval(5000); // 5 Seconds

Event Handling

SocketConnect provides access to all status updates from XMLHttpRequest. You can add any event handler to them using the below:

// Response code 0
connection.reqNotInit = function() {
  console.log('Request not initialised.');
};
Response CodeMeaningSocketConnect Function
0Request not initialisedreqNotInit
1Server connection establishedconnectionEstablished
2Request receivedreqReceived
3Processing requestprocessingReq

Response code 4 is dealt with within the shared API handleData.

Keywords

FAQs

Last updated on 06 Mar 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc