WebSocket Server
This plugin allows you to run a single, lightweight, barebone WebSocket Server.
https://github.com/becvert/cordova-plugin-websocket-server
Stuck on a Cordova issue?
If you're building a serious project, you can't afford to spend hours troubleshooting. Ionic’s experts offer premium advisory services for both community plugins and premier plugins.
Installation#
- Capacitor
- Cordova
- Enterprise
$ npm install cordova-plugin-websocket-server $ npm install @ionic-native/web-socket-server $ ionic cap sync$ ionic cordova plugin add cordova-plugin-websocket-server $ npm install @ionic-native/web-socket-server Ionic Enterprise comes with fully supported and maintained plugins from the Ionic Team. Learn More or if you're interested in an enterprise version of this plugin Contact Us
Supported Platforms#
- Android
- iOS
Usage#
React#
Learn more about using Ionic Native components in React
Angular#
import { WebSocketServer } from '@ionic-native/web-socket-server';
constructor(private wsserver: WebSocketServer) { }
...
// start websocket serverthis.wsserver.start(8888, {}).subscribe({ next: server => console.log(`Listening on ${server.addr}:${server.port}`), error: error => console.log(`Unexpected error`, error);});
// watch for any messagesthis.wsserver.watchMessage().subscribe(result => { console.log(`Received message ${result.msg} from ${result.conn.uuid}`);});
// send message to connection with specified uuidthis.wsserver.send({ uuid: '8e7c4f48-de68-4b6f-8fca-1067a353968d' }, 'Hello World');
// stop websocket serverthis.wsserver.stop().then(server => { console.log(`Stop listening on ${server.addr}:${server.port}`);});