Skip to content

Custom implementation of WebSocket client for react-native

Notifications You must be signed in to change notification settings

GamgeeNL/react-native-websocket-impl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-websocket-impl

Enables WebSocket ping and pong messages to be sent from js code.

Getting started

$ npm install react-native-websocket-impl --save

Mostly automatic installation

$ react-native link react-native-websocket-impl

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-websocket-impl and add RNWebSocketImpl.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNWebSocketImpl.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.iskander508.WebSocketImpl.RNWebSocketImplPackage; to the imports at the top of the file
  • Add new RNWebSocketImplPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-websocket-impl'
    project(':react-native-websocket-impl').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-websocket-impl/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-websocket-impl')
    

Usage

import openConnection from 'react-native-websocket-impl';

openConnection(url, headers)
  .then(socket => {
    console.log('Socket created successfully!')
    
    socket.on('open', () => console.log('OPEN'))
    socket.on('message', (message) => console.log('MSG: ' + message))
    socket.on('close', (code, reason) => console.log('CLOSE'))
    socket.on('error', (message) => console.log('ERROR'))
    
    socket.send('message')
    socket.ping() // send WS PING frame
    socket.pong() // send WS PONG frame (unsolicited)
    socket.close()
  }, err => {
    console.log('Socket creation failed!')
  });

About

Custom implementation of WebSocket client for react-native

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published