Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ngx-multi-window
Advanced tools
Cross-window communication for multi-window angular applications
Pull-based cross-window communication for multi-window angular applications
First you need to install the npm module:
npm install ngx-multi-window --save
For older angular versions you may install previous versions of this library:
ngx-multi-window version | compatible angular version |
---|---|
0.6.1 | 16 |
0.6 | 15 |
0.5 | 14 |
0.4.1 | 8 - 13 |
0.3.2 | 7 |
0.2.4 | 6 |
Then add the MultiWindowModule
to the imports array of your application module:
import {MultiWindowModule} from 'ngx-multi-window';
@NgModule({
imports: [
/* Other imports here */
MultiWindowModule
]
})
export class AppModule {
}
Finally, you need to specify how your application should load the ngx-multi-window library:
Inject the MultiWindowService
into your component or service.
import {MultiWindowService} from 'ngx-multi-window';
export class AppComponent {
constructor(private multiWindowService: MultiWindowService) {
// use the service
}
}
You may inject a custom MultiWindowConfig
object when importing the MultiWindowModule
into your application.
@NgModule({
imports: [
...
MultiWindowModule.forRoot({ heartbeat: 542 })
],
})
Check the description of the MultiWindowConfig interface properties for options. The default options are
{
keyPrefix: 'ngxmw_',
heartbeat: 1000,
newWindowScan: 5000,
messageTimeout: 10000,
windowTimeout: 15000,
windowSaveStrategy: WindowSaveStrategy.NONE,
}
Every window has a unique, unchangeable id which can be accessed via multiWindowService.id
.
In addition to that every window as a changeable name which can be get/set
via multiWindowService.name
.
Receive messages addressed to the current window by subscribing to the observable returned from
multiWindowService.onMessages()
:
import { MultiWindowService, Message } from 'ngx-multi-window';
class App {
constructor(private multiWindowService: MultiWindowService) {
multiWindowService.onMessage().subscribe((value: Message) => {
console.log('Received a message from ' + value.senderId + ': ' + value.data);
});
}
}
Send a message by calling multiWindowService.sendMessage()
:
import { MultiWindowService, WindowData, Message } from 'ngx-multi-window';
class App {
constructor(private multiWindowService: MultiWindowService) {
const recipientId: string; // TODO
const message: string; // TODO
multiWindowService.sendMessage(recipientId, 'customEvent', message).subscribe(
(messageId: string) => {
console.log('Message send, ID is ' + messageId);
},
(error) => {
console.log('Message sending failed, error: ' + error);
},
() => {
console.log('Message successfully delivered');
});
}
}
The message returns an observable which will resolve with a message id once the message has been send (= written to local storage).
The receiving window will retrieve the message and respond with a MessageType.MESSAGE_RECEIVED
typed message.
The sending window/app will be informed by finishing the observable.
In case no MessageType.MESSAGE_RECEIVED
message has been received by the sending window
within a certain time limit (MultiWindowConfig.messageTimeout
, default is 10s)
the message submission will be canceled. The observable will be rejected and the
initial message will be removed from the current windows postbox.
To get the names and ids of other window/app instances the MultiWindowService
offers two methods:
multiWindowService.onWindows()
returns an observable to subscribe to in case you require periodic updates of the
fellow windows. The observable will emit a new value every time the local storage has been scanned for the windows.
This by default happens every 5 seconds (MultiWindowConfig.newWindowScan
).
Use multiWindowService.getKnownWindows
to return an array of WindowData
.
No special handling is necessary to open new windows. Every new window/app will register itself
by writing to its key in the local storage. Existing windows will identify new windows
after MultiWindowConfig.newWindowScan
at the latest.
The MultiWindowService
offers a convenience method newWindow()
which provides details for the
new window's start url. If used the returned observable can be utilized to get notified
once the new window is ready to consume/receive message.
The library comes with a mechanism to save the window id using the browser's window.name
property. This
property is persisted on page reloads, resulting in the same tab/window running your angular application to keep
the ngx-multi-window id even when reloading the page.
Note: Only the window id is persisted, the customizable window name and messages are kept in the local storage,
but are automatically rediscovered by the new window once it starts consuming messages.
To save the window id, set the respective config property nameSafeStrategy
to the desired value. Additionally
one needs to call saveWindow()
function e.g. during window unloading by attaching a HostListener
in your
main AppComponent.
@HostListener('window:unload')
unloadHandler() {
this.multiWindowService.saveWindow();
}
This library is based on "pull-based" communication. Every window periodically checks the local storage for messages addressed to itself. For that reason every window has its own key in the local storage, whose contents/value looks like:
{"heartbeat":1508936270103,"id":"oufi90mui5u5n","name":"AppWindow oufi90mui5u5n","messages":[]}
The heartbeat is updated every time the window performed a reading check on the other window's local storage keys.
Sending message from sender A to recipient B involves the following steps:
This project contains a demo application that has been adapted to showcase the functionality of ngx-multi-window. Run the demo app by checking out that repository and execute the following command in the project root directory:
npm install
ng serve
FAQs
Cross-window communication for multi-window angular applications
The npm package ngx-multi-window receives a total of 761 weekly downloads. As such, ngx-multi-window popularity was classified as not popular.
We found that ngx-multi-window 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.