SOLNM
  • Introduction
  • Technology
    • CMv3 & UI
    • Dapp
    • Wallet adapter
    • WebSockets
    • React app
    • Web3.js
    • Metaplex
    • Mpl Candy Machine
    • JSON-RPC
    • Geyser plugin postgres
  • Drops
    • Basic Drop
    • Plus Drop
    • Advanced Drop
  • NFT Tickets solution
  • NFT Payments
  • Collect Whitelists
  • NFT assets
  • Fees and Commission
  • SOLPAD PASS
  • SOLNM SPL Token
  • Airdrop
  • Whitepaper
  • Roadmap
  • FAQ
  • Terms
  • Privacy
  • Jobs
    • Affiliate Marketing
  • Linkedin
  • Twitter
  • Telegram
  • Discord
  • Github
Powered by GitBook
On this page
  • Documentation
  • OSS Features
  1. Technology

WebSockets

PreviousWallet adapterNextReact app

Last updated 1 year ago

Install our OSS library in your project:

npm install rpc-websockets

Write your source code using rpc-websockets:

var WebSocket = require('rpc-websockets').Client
var WebSocketServer = require('rpc-websockets').Server

// instantiate Server and start listening for requests
var server = new WebSocketServer({
  port: 8080,
  host: 'localhost'
})

// register an RPC method
server.register('sum', function(params) {
  return params[0] + params[1]
})

// ...and maybe a protected one also
server.register('account', function() {
  return ['confi1', 'confi2']
}).protected()

// create an event
server.event('feedUpdated')

// get events
console.log(server.eventList())

// emit an event to subscribers
server.emit('feedUpdated')

// close the server
server.close()

// instantiate Client and connect to an RPC server
var ws = new WebSocket('ws://localhost:8080')

ws.on('open', function() {
  // call an RPC method with parameters
  ws.call('sum', [5, 3]).then(function(result) {
    require('assert').equal(result, 8)
  })

  // send a notification to an RPC server
  ws.notify('openedNewsModule')

  // subscribe to receive an event
  ws.subscribe('feedUpdated')

  ws.on('feedUpdated', function() {
    updateLogic()
  })

  // unsubscribe from an event
  ws.unsubscribe('feedUpdated')

  // login your client to be able to use protected methods
  ws.login({'username': 'confi1', 'password':'foobar'}).then(function() {
    ws.call('account').then(function(result) {
      require('assert').equal(result, ['confi1', 'confi2'])
    })
  }).catch(function(error) {
    console.log('auth failed')
  })

  // close a websocket connection
  ws.close()
})

Documentation

Please consult our API documentation for both the WebSocket server and client JavaScript and TypeScript classes.

OSS Features

Features of the free open-source edition.

All library's open-source features are documented in our API documentation and can be used free of charge. You are free to implement your solutions based on provided methods in any way you are comfortable with, as long as you use our work along our very permissive conditions.

Fork from
license