through all pages


Hi all,

I am using plugin and it works perfectly fine. I just was wondering if you have any ideas how to implement it through all pages so I don’t have to connect in every page and socket variable would be defined. Any ideas or solutions are highly appreciated. Thanks


Sorry for the hasty and short reply but here goes:

NativeScript modules (files and exported scripts) you require or import are cached in memory, so that if you require a module more than once, you will get the same cached exported functions and classes every time. Keeping that in mind you can implement the connection as a singleton instance, which you just getSocketConnection() for example whenever you want to emit a message to the other end, or change the onRequest callback logic.


@Pete.K thank you for prompt reply. I tired to implement singleton connection like this:

export var socket = SocketIO.connect('https://xxxxxx:3001', <SocketIO.SocketOptions>{
       log: true,
       secure: true,
    query: 'token=xxxxx',        

but it is only getting connected if I switch off and switch on the app. If I logout and login in it doesn't connect anymore. If I move the same code to components it gets connected multiple times. I need single connection and shared instance afterwards, I just don't know how to implement it. I would be grateful if you could assist with this issue. Thanks


@bachras i use a file “connect.js” with all socketio logic, then require it in home page so that it connects on App start and use observable array in a viewmodel to use in a Chat page, i also require it in my background mode, so in short, one file with all socket logic

 var model=require("./main-view-model");
 var socketIo=require("");
 var Vibrate = require("nativescript-vibrate").Vibrate;
var vibrator = new Vibrate();
 var io= new socketIo.connect('',{'forceNew':true});
 var ObservableArray = require ("data/observable-array");
   model.Chatview=new ObservableArray.ObservableArray([]);
   model.Info=new ObservableArray.ObservableArray([]);
 var username="mmmmi";
   io.on('connect', function(){
    console.log(username+'  is connected  ');
    io.emit('new_client', username,function(data){
           io.emit('check',username ) ;
            alert('Error! Use different name') ;
      io.on('new_client', function(data,id) {
        for (var i = 0; i < data.length; i++) {
            Arrz=[{name:,id: model.ids}];
            console.log('names: ','id\'s  :'+model.ids);
 exports.sendText=function (args) {
 const data= {text: model.ctext,username: username}
   // io.emit('message',data);
        if(m!=='') {
            for (var i = 0; i < cnt; i++) {
               var dateD=new Date(t);
               model.time=timeD;//fecha.format(dateD, ' dddd, H:mm');
                if (u == username) {
                    model.chatClass='msg me';
                   model.chatClass= 'msg them';
                Arr=[{msg: model.msg,chatClass: model.chatClass,time:  model.time,name:}];

everything is exported usind Model or exports!


If this is the solution, perhaps mark it as such?