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

@lawcket/react-websocket

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lawcket/react-websocket

A slim React websocket implementation

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm downloads total npm version npm license

@lawcket/react-websocket

A slim Websocket implementation for React 16+. The libray uses the standard browser WebSocket so 'should' be supported in React-Native.

Installation

npm install --save @lawcket/react-websocket

or

yarn add @lawcket/react-websocket

Usage

Example:

import React, { useState } from 'react';
import Websocket from '@lawcket/react-websocket';

const Messages = () => {
  const [messages, addMessage] = useState([]);
  return (
    <Websocket 
      url='wss://...' 
      retry
      onOpen={({ send }) => console.log('open')}
      onClose={({ code, reason }) => console.log('closed', code, reason)}
      onMessage={({ send, data }) => console.log('message', data)}
      onError={(error) => console.log('error', error)} >
      {({ send, close }) => {
        return (
            <pre>{JSON.stringify(messages, null, 2)}</pre>
        );
      }}
    </Websocket>
  );
}

PropTypes

NameTypeDescriptionDefault
urlstringThe websocket url to connect to
protocolstringThe protocol for the websocket to use
retrybooleanIf an error occurs, should it reconnecttrue
onOpenfunctionNotifies when the connection has been opened
onMessagefunctionNotifies when an incoming message has been received
onClosefunctionNotifies the connection was closed
onErrorfunctionNotifies of an error with the connection, will close the connection

onOpen

ParameterTypeDescription
sendfunction(any)A function to send data to the server

onMessage

ParameterTypeDescription
dataanyIncoming message (usually stringified JSON data)
sendfunction(any)A function to send data to the server

onError

ParameterTypeDescription
errorErrorThe error that occured during connection or usage

onClose

Reference: Close codes & reasons

ParameterTypeDescription
codenumberThe close code sent by the server
reasonstringThe reason the connection was closed

Keywords

FAQs

Package last updated on 04 May 2019

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