
Product
Announcing Precomputed Reachability Analysis in Socket
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
@pushprotocol/socket
Advanced tools
This package gives access to Push Protocol (Push Nodes) using Websockets built on top of [Socket.IO](https://socket.io/docs/v4/client-api/). Visit [Developer Docs](https://docs.push.org/developers) or [Push.org](https://push.org) to learn more.
This package gives access to Push Protocol (Push Nodes) using Websockets built on top of Socket.IO. Visit Developer Docs or Push.org to learn more.
yarn add @pushprotocol/socket@latest ethers@^5.6
or
npm install @pushprotocol/socket@latest ethers@^5.6
import {
createSocketConnection,
EVENTS
} from '@pushprotocol/socket';
In any of the below methods (unless explicitly stated otherwise) we accept either -
CAIP format: for any on chain addresses We strongly recommend using this address format. Learn more about the format and examples.
(Example : eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb
)
ETH address format: only for backwards compatibility.
(Example: 0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb
)
Note - For chat related apis, the address is in the format: eip155:<address> instead of eip155:<chainId>:<address>, we call this format Partial CAIP
(Example : eip155:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb
)
const pushSDKSocket = createSocketConnection({
user: 'eip155:5:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb', // CAIP, see below
env: 'staging',
socketOptions: { autoConnect: false }
});
const pushSDKSocket = createSocketConnection({
user: 'eip155:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb',
env: 'staging',
socketType: 'chat',
socketOptions: { autoConnect: true, reconnectionAttempts: 3 }
});
IMPORTANT: create the connection object in your app only when you have the user
address available since its mandatory.
autoConnect
: Generally if we don't pass autoConnect: false
then the socket connection is automatic once the object is created. Now since we may or may not have the account address handy and wish to start the connection during instantiation so this option makes it easier for us to choose when we want to connect
or not!
Allowed Options (params with * are mandatory)
Param | Type | Default | Remarks |
---|---|---|---|
user* | string | - | user account address (CAIP) |
env | string | 'prod' | API env - 'prod', 'staging', 'dev' |
socketType | 'notification' | 'chat' | 'notification' | socket type |
socketOptions | object | - | supports the same as SocketIO Options |
pushSDKSocket.connect();
pushSDKSocket.disconnect();
pushSDKSocket.on(EVENTS.CONNECT, () => {
});
pushSDKSocket.on(EVENTS.DISCONNECT, () => {
});
pushSDKSocket.on(EVENTS.USER_FEEDS, (feedItem) => {
// feedItem is the notification data when that notification was received
});
pushSDKSocket.on(EVENT.CHAT_RECEIVED_MESSAGE, (message) => {
// message is the message object data whenever a new message is received
});
pushSDKSocket.on(EVENT.CHAT_GROUPS, (message) => {
// message is the message object data whenever a group is created or updated
});
Supported EVENTS
EVENT name | When is it triggered? |
---|---|
EVENTS.CONNECT | whenever the socket is connected |
EVENTS.DISCONNECT | whenever the socket is disconneted |
EVENTS.USER_FEEDS | whenever a new notification is received by the user after the last socket connection |
EVENTS.USER_SPAM_FEEDS | whenever a new spam notification is received by the user after the last socket connection |
EVENT.CHAT_RECEIVED_MESSAGE | whenever a new message is received |
EVENT.CHAT_GROUPS | whenever a group is created or updated |
import { useState, useEffect } from "react";
import { createSocketConnection, EVENTS } from '@pushprotocol/socket';
const user = '0xD8634C39BBFd4033c0d3289C4515275102423681';
const chainId = 5;
const userCAIP = `eip155:${chainId}:${user}`;
function App() {
const [sdkSocket, setSDKSocket] = useState<any>(null);
const [isConnected, setIsConnected] = useState(sdkSocket?.connected);
const addSocketEvents = () => {
sdkSocket?.on(EVENTS.CONNECT, () => {
setIsConnected(true);
})
sdkSocket?.on(EVENTS.DISCONNECT, () => {
setIsConnected(false);
})
sdkSocket?.on(EVENTS.USER_FEEDS, (feedItem) => {
/**
* "feedItem" is the latest notification received
*/
console.log(feedItem);
})
};
const removeSocketEvents = () => {
sdkSocket?.off(EVENTS.CONNECT);
sdkSocket?.off(EVENTS.DISCONNECT);
};
const toggleConnection = () => {
if (sdkSocket?.connected) {
sdkSocket.disconnect();
} else {
sdkSocket.connect();
}
};
useEffect(() => {
if (sdkSocket) {
addSocketEvents();
}
return () => {
removeSocketEvents();
};
}, [sdkSocket]);
useEffect(() => {
const connectionObject = createSocketConnection({
user: userCAIP,
env: 'dev',
socketOptions: { autoConnect: false }
});
setSDKSocket(connectionObject);
return () => {
if (sdkSocket) {
sdkSocket.disconnect();
}
};
}, []);
return (
<div>
<h1>Socket Hello World</h1>
<div>
<p>Connection Status : {JSON.stringify(isConnected)}</p>
<button onClick={toggleConnection}>{isConnected ? 'disconnect' : 'connect'}</button>
</div>
</div>
);
}
export default App;
Please note connecting with sockets and maintaining the state of the connection object in your DAPP might have a different setup like first getting the user
account and chainId
and then connecting with socket. You can use React Context for state management.
FAQs
This package gives access to Push Protocol (Push Nodes) using Websockets built on top of [Socket.IO](https://socket.io/docs/v4/client-api/). Visit [Developer Docs](https://docs.push.org/developers) or [Push.org](https://push.org) to learn more.
The npm package @pushprotocol/socket receives a total of 41 weekly downloads. As such, @pushprotocol/socket popularity was classified as not popular.
We found that @pushprotocol/socket 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.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.