
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-websokkit
Advanced tools
A declarative Render Prop Component and Higher Order Component (HOC) for react which deals with websockets and the STOMP subprotocol in a flexible way. Inspired by the excellent react-apollo.
react-websokkit allows you to:
Make any component or app websocket aware
Re-render your component with new data in real time when it is pushed over the websocket from the server
Push data through the websocket to the server from your react component
Show an error message when something goes wrong
Interact with different topics through use of the STOMP sub-protocol
npm
npm i react-websokkit
yarn
yarn add react-websokkit
There are 2 main ways to use react-websokkit. You can either make use of the render prop component, or the HOC (Higher Order Component). Both do the same thing, however they have a slightly different API.
Importing from react-websokkit
will by default import the unminified, transpiled ES5 code into your application which you can then bundle yourself.
If you want to pull in the already minified, production bundle, you will need to import from react-websokkit/dist/react-websokkit.js
, or create an alias in your bundler configuration.
url
: String - The url of the websocket server you want to connect to.
subscriptions
: String[] - The STOMP topics you want the component to subscribe to.
render
: (props) => JSX.Element - The render prop is what you want to render. All the websocket related props (data, error) will be passed down to your component.
import React, { Component } from "react";
import Socket from "react-websokkit";
const Foo = () => {
return (
<Socket
url={"url/to/websocket/server"}
subscriptions={["/some/topic"]}
render={({ data: { response, error }, send }) => {
return (
<span> Data: { response && response }</span>
<span> Error: { error && error }</span>
<button onClick={() => send("some/topic", { name: "hey" })}>
);
}}
/>
)
}
export default Foo;
The higher order component takes the same props as the render prop component, but in an object. See the example below.
import React, { Component } from "react";
import { withSokkit } from "react-websokkit";
const Foo = ({ data: { response, error }, send }) => {
return (
<span> Data: { response && response }</span>
<span> Error: { error && error }</span>
<button onClick={() => send("some/topic", { name: "hey" })}>
);
}
export default withSokkit({
url: "url/to/websocket/server",
subscriptions: ["/some/topic"]
})(Foo);
When you render a component using react-websokkit
, you will be provided with some props. Every time data is pushed through the websocket to your component, it will cause a state update and re-render. These are as follows.
data
: Object - Contains either a response
or error
key based on whether or not there was a failure when data was pushed through the websocket on the subscribed topics.
send
: (topic: String, payload: Object | String) => void - This function allows you to send data to the server on a STOMP topic over a websocket from inside your component.
Clone the repo and install dependencies with your favourite package manager. The NPM scripts in the package.json
are below.
build:minified
=> builds and minifies the code and outputs a production ready bundleclean
=> blows away build folders (lib
, dist
) for a clean builddev
=> runs a webpack
development server with hot reloading for development. Access the development playground at localhost:8081
prepublish
=> runs prepublish:compile
when executing a npm publish
prepublish:compile
=> run clean
, transpile
and then build:minified
test
=> runs the jest test suite.test:watch
=> run test
in watch
mode, which will re-run when you change files pertaining to your tests.test:update
=> run test
, but update outdated jest snapshotstranspile
=> transpile all files in src
into the lib
folder using babelFAQs
React component for providing real time data through websockets
We found that react-websokkit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.