Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sockjs-client

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sockjs-client

SockJS-client is a browser JavaScript library that provides a WebSocket-like object.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.9M
increased by37.56%
Maintainers
1
Weekly downloads
 
Created

What is sockjs-client?

The sockjs-client npm package provides a browser JavaScript library that provides a WebSocket-like object that offers a consistent, cross-browser interface for real-time, bi-directional communication between the client and the server. It falls back to a variety of browser-specific transport protocols if WebSockets are not available.

What are sockjs-client's main functionalities?

Establishing a connection to a SockJS server

This code sample demonstrates how to establish a connection to a SockJS server. It creates a new SockJS client instance, sets up event listeners for 'open', 'message', and 'close' events, and logs information to the console when these events occur.

var sock = new SockJS('http://mydomain.com/my_prefix');
sock.onopen = function() {
  console.log('open');
};
sock.onmessage = function(e) {
  console.log('message', e.data);
};
sock.onclose = function() {
  console.log('close');
};

Sending messages to the server

This code sample shows how to send a message to the server using the SockJS client. It sends a JSON stringified object as the message content.

sock.send(JSON.stringify({message: 'Hello, server!'}));

Receiving messages from the server

This code sample illustrates how to receive messages from the server. It sets up an event listener for the 'message' event and logs the received message to the console after parsing the JSON data.

sock.onmessage = function(e) {
  var message = JSON.parse(e.data);
  console.log('Received message:', message);
};

Closing the connection

This code sample demonstrates how to close the connection to the server using the SockJS client.

sock.close();

Other packages similar to sockjs-client

Keywords

FAQs

Package last updated on 24 Sep 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc