한국어
오픈소스포럼
 이곳은 다양한 오픈소스 프로젝트를 소개하고 리뷰, 활용 방법을 공유합니다.

Electron (일렉트론)이라는 node.js 기반의 소프트웨어 프레임 워크를 사용하면 HTML, CSS, JavaScript 등의 Web 기술을 사용하여 Windows 나 Mac, Linux를 지원하는 데스크톱 응용프로그램을 개발할 수 있다.

 

Redis 는 In Memory 키-값 구조의 데이터 스토어, 캐시 그리고 어느 정도는 메시지 브로커 기능을 한다. 여기서는 Redis의 많은 기능 중에서 라즈베리파이에서 Publish/Subscribe 패턴을 사용해 본다. 

 

구현 사항을 정리하면 다음과 같다. (설치 과정등은 생략한다.)

 

간단한 Electron 응용프로그램에서는 채널 구독과 수신한 메세지를 출력하는 역할을 수행하고 다른 서비스(여기서는 다른터미널)에서 채널에 어떤 메세지를 발행하는 구조다.

 

다음은 간단하게 작성한 HTML과 JS 소스코드다. 

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
    <h1>Hello World!</h1>
    <p>
        We are using node <script>document.write(process.versions.node)</script>,
        Chrome <script>document.write(process.versions.chrome)</script>,
        and Electron <script>document.write(process.versions.electron)</script>.
    </p>
     <div style="width:300px; height:200px; border:10px solid gray">
        <p id="message" style="text-align: center;">will be going to display message.</p>
    </div>
         <script>
            require('./subscribe')
        </script>
</body>
</html>

 

subscribe.js

const redis = require("redis");
 
const subscriber = redis.createClient();

const ch = 'ch0'
 
subscriber.on("message", function(channel, message) {
    const msg = "Subscriber received message in channel '" + channel + "': " + message;
    console.log(msg);
    document.getElementById('message').textContent = msg;
});
 
subscriber.subscribe(ch);

 

subscribe.js 에서는 구독하려는 채널의 메시지를 수신 하기위해 on 함수를 통해 이벤트 콜백을 등록한다. 콜백에서는 ch0 채널에 대한 메세지를 수신하고 사각형 안쪽의 텍스트를 바꾼다.

 

클라이언트 사이드에서 사용할 수 있는 더 많은 이벤트는 다음을 참고한다 : https://www.npmjs.com/package/redis#subscriber-events

 

Electron 응용프로그램을 실행하면 처음에는 사각형 상자에 "will be going to display message." 라는 텍스트를 출력하도록 한다.

2021-01-31-125630_1080x1920_scrot.png

 

다른 터미널을 열고 다음과 같이 ch0 채널에 PUBLISH 명령으로 메세지를 발행한다.

redis-PUBLISH.png

 

그러면 Electron 응용프로그램에는 다음과 같이 표시된다.

2021-01-31-125702_1080x1920_scrot.png

 

번호 제목 글쓴이 날짜 조회 수
35 Wayland IVI Extension 간단 리뷰 file makersweb 2019.05.12 2256
34 GDBus 튜토리얼(GDBus tutorial) file makersweb 2019.06.30 10342
33 텔레그램(Telegram) Bot 개발 file makersweb 2019.07.21 5706
32 리눅스에서 SDL2 최신버전 컴파일과 Qt Creator로 개발환경 구성 file makersweb 2019.10.06 3035
31 webOS소개 및 Raspberry Pi 3 에서 실행 file makersweb 2019.10.13 3875
30 도커(docker)설치 및 기본 명령어 makersweb 2019.12.02 482
29 Pluma(C++ Plug-in Management Framework) 튜토리얼 file makersweb 2019.12.07 13981
28 [SDL2 와 OpenGL]윈도우 생성과 2D그래픽 file makersweb 2020.04.11 3058
27 ZeroMQ의 기본 메세지 패턴들 file makersweb 2020.07.31 8481
26 ZeroMQ 비동기 클라이언트/서버 패턴 file makersweb 2020.08.13 1768
25 Qt와 GStreamer 로 작성한 flac 오디오 재생 예제 file makersweb 2020.09.05 1139
24 가볍고 쉬운 임베디드용 그래픽 라이브러리 - LVGL file makersweb 2020.09.16 3321
23 윈도우에서 안드로이드 flutter 프로그래밍 개발환경 구축(with Visual Studio Code) file makersweb 2020.09.16 794
22 GENIVI DLT(Diagnostic Log and Trace) 활용 file makersweb 2020.11.19 8313
21 Dear ImGui, 경량의 C++ 용 GUI 및 Widget 라이브러리 file makersweb 2020.11.28 9057
20 라즈베리파이4에서 openFrameworks 예제 실행 file makersweb 2020.12.13 559
19 Nana, C++용 크로스플랫폼 GUI 라이브러리 file makersweb 2021.01.06 2066
» 라즈베리파이에서 Redis의 Pub/Sub 패턴을 사용하는 Electron 응용프로그램 file makersweb 2021.01.31 681
17 Protocol Buffers 를 이용한 직렬화 with Conan Package Manager file makersweb 2021.02.24 863
16 CANdevStudio 를 사용하여 CAN 네트워크 시뮬레이션 file makersweb 2021.03.09 1701