رویداد جلسات باز نرمافزاری خوشبختانه همچنان به قوت خودش ادامه دارد. رویدادی که دو بار در ماه برگزار میشود و در هر جلسه به موضوع جدیدی پرداخته میشود. تا به امروز جلسات باز نرمافزاری در موضوعات مختلفی مانند بلاکچین، Deep Learning، خوشحال سازی مشتری و UX Writing، ریاکت نیتیو (React Native) و... برگزار شده است. در جلسه پنچم جلسات باز نرمافزاری، که با حضور برنامه نویسان رایچت همراه بود، به موضوع Node.js اختصاص یافت.
سامان بابلی، هم بنیانگذار و CTO (مدیر فنی) رایچت، و حامد قلی زاده، هم بنیانگذار و CIO (مدیر فناوری اطلاعات) رایچت، در این جلسه عهده دار ارائه مطالب در مورد Nodejs بودند. سامان بابلی در آغاز به معرفی و مفاهیم nodejs پرداخت و در ادامه حامد قلیزاده با اجرای پروژه، بصورت عملی استفاده و قابلیت های Nodejs را نمایش داد.
جلسه پنجم جلسات باز نرمافزاری تبریز: رایچتی ها از Node.js میگویند - فناوری به وقت تبریز
Node.js چیست ؟
Node.js نه یک زبان برنامه نویسی است، نه یک فریمورک. Node.js یک محیط است که به ما اجازه می دهد کدهای جاوااسکریپت رو در سمت سرور یا ماشین اجرا بکنیم. کدهای Node.js توسط موتور V8 گوگل به زبان ماشین تبدیل میشود.
چرا از Nodejs ؟
بطور عمده امروزه Node.js در مواردی که نیاز به پاسخ به حجم زیاد درخواست ها است، استفاده می٬شود.
دلیل استفاده فراوان از NodeJS، سریع بودن آن است که به لطف Non Bloking I/O و async بودن امکان پذیر شده است. برای درک بهتر این موضوع به یک مثال ساده Async میپردازیم. فرض کنید کارمندی در یک اداره برای تکمیل یک گزارش نیاز به دریافت اطلاعاتی از بازرس دارد. زمانی که این کارمند با بازرس تماس میگیرد و اطلاعات را از او درخواست میکند، بازرس در پاسخ میگوید که به نیم ساعت زمان برای تحویل اطلاعت نیاز دارد. کارمند تا زمان دریافت اطلاعات پشت خط می ماند و تلفن در طول مدت دریافت اطلاعات مشغول است (sync) اما اگر اطلاعات را از بازرس بخواهد و بخواهد بعد از آماده شدن با او تماس بگیرد، میتواند در این نیم ساعت کارهای دیگرش را انجام دهد و یا تماس های دیگری بگیرد و … و بعد از نیم ساعت بازرس تماس بگیرد و اطلاعات را به کارمند بدهد. همانطور که دیدید در اینجا چیزی بلاک نشد (async)
از بهترین ویژگیهای Node.js این است که میتوانید هم در سمت کلاینت(مرورگر) و هم در سمت سرور از یک زبان واحد به نام جاوااسکریپت استفاده کنید. پس زمان کمتری برای یادگیری نیاز خواهید داشت. همچنین برای راه اندازی MVP ها بسیار عالیست چرا که میتوانید نسخه مخصوص تمامی پلتفرمها را به کمک آنها راهاندازی کنید و زمان خیلی کمتری برای آمادهسازی آنها صرف کنید.
از برتری های دیگر نود جی اس، همراهی اکوسیستم بزرگ و فعال از برنامه نویسان است که تعداد بسیار زیادی ماژول، فریمورک و آموزش را برای Nodejs فراهم کردهاند. تمامی ماژول های ساخته شده در سایت npmjs قرار میگیرند. npm اختصار عبارت node package manager است.
هر پروژه دارای یک فایل Package.jason است که به عنوان "شناسنامه پروژه" از آن یاد میشود. در این فایل اطلاعات کلی در مورد پروژهی ما یعنی نام، ورژن، توضیحات و … نگه داری می شود، و همچنین دارای قسمتی است که نام و ورژن ماژولهای استفاده شده را ذخیره میکند تا در صورت اجرای پروژه توسط سایر اعضای تیم یا بر روی یک سیستم دیگر با اجرای دستور زیر بتوان تمام ماژول ها را نصب کرد.
$npm install
در ادامه سامان بابلی به معرفی چند ماژول پرکاربرد و معروف در npm پرداخت. ماژول هایی مانند: express, hapi, socket.io, redis, pm2
template Engine چیست؟
تمپلیت انجین ها امکانات و متفاوتی را ارائه میدهند و معمولا کار کردن با دیتاها در سمت view را برای ما راحتتر میکنند. به عنوان مثال تمپلیت انجین pug سینتکس کدهای html را برای ما سادهتر کرده و همچنین کار با دیتاها نیز آسان شده. از ویژگی های بارز این تمپلیت انجین ها پشتیبانی از حلقه ها و یا if هست. برای درک بهتر میتوانید به مثال درون اسلاید ها مراجعه کنید.
فریمورک های nodejs
express: یکی از معروف ترین فریمورک های nodejs است.
Koa: تیم توسعه دهنده express توسعه آن را بر عهده داشت و تمرکز بیشتری بر روی generator ها دارد.
sails js: بر پایه معماری MVC است و شبیه Ruby on rails هست.
hapi: فریمورک دیگری برای nodejs است که لول بالاتری نسبت به express و koa دارد و ساخت اپلیکیشن ها و سرویس ها را راحتتر کرده است.
ES6 ورژن 2015 از ECMAScript است. ECMAScript نام یک استاندارد است که جاوااسکریپت از آن پیروی میکند. هر ورژن جدید از ECMAScript دارای ویژگی های جدیدی است که تا امروز ورژن ES8 منتشر شده است اما آخرین نسخهای که توسط nodejs پشتیبانی میشود، ورژن ES6 است.
تفاوت web socket و http
در ارتباط از طریق پروتکل http در بازههای زمانی مرتب باید درخواست هایی از سمت کلاینت به سرور ارسال شود تا در صورت وجود دیتای جدید، آن را دریافت کند. اما در ارتباط از طریق پروتکل web socket یک ارتباط همیشگی بین کلاینت و سرور برقرار است که به ایونت ها گوش میدهند یا میتوانند ایونت ها را به یکدگیر ارسال کنند. به تصویر موجود در اسلاید مراجعه کنید.
از Nodejs استفاده نکنید اگر
در برنامه هایی که مصرف CPU بالا یا به عبارتی محاسبات زیادی دارند، مانند پردازش تصویر. همچنین بهتر است برای برگرداندن فایل های استاتیک به کاربراز nginx استفاده کنید و این کار را به Node.js نسپارید.
از nodejs استفاده کنید در
در اپ های Real Time مانند اپ های چت یا داشبورد هایی که نیاز به بروزرسانی و نمایش مداوم دیتای جدید به کاربر دارد nodejs انتخاب خوبی است. اپلیکیشن چت مثال خوبی برای Node.js هست چرا که حجم دیتای در حال انتقال و همچنین ترافیک زیادی دارد و نیازی به مصرف CPU ندارد. .
در ادامه ارائه سامان بابلی به موضوع load balancing در نود جی اس اشاره کرد. در یک جمله میتون لود بالانسینگ را تقسیم رکوست های ورودی به چند سرور تعریف کرد. برای درک بهتر این موضوع میتوانید مطلبی را که در Medium نوشته است را مطالعه کنید.
به عنوان موضوعات پایانی به معرفی شرکتهایی که از nodejs استفاده میکنند اشاره شد. مانند Uber که تقریبا به عنوان یکی از اولین و بزرگترین استفاده کننده های nodejs شناخته میشود، که در این لینک اطلاعات بیشتری در این مورد موجود هست. همچنین کسب و کار هایی مانند paypal، لینکدین، یاهو و ... چندین مورد از این شرکت ها هستند.
برای دانلود اسلایدهای ارائه سامان بابلی اینجا کلیک کنید.
پس از پایان ارائه ی سامان بابلی، حامد قلیزاده مدیر فناوری اطلاعات رایچت مفاهیم نود جی اس را به صورت عملی برای حضار نشان داد.
حامد قلیزاده - جلسه پنچم جلسات باز نرمافزاری
قلیزاده ارائه خود را با مقدمهای از Nodejs آغاز کرد. ابتدا نحوه عملکرد Nodejs و ماژولهای Nodejs را به صورت عملی نشان داد. سپس کاربرد های فایل شناسنامه (package.json) را توضیح داد و سپس یک فایل شناسنامه ایجاد کرد. بعد از آن نحوهی کار با npm را توضیح داد و برای مثال ماژول lodash را نصب کرد و در یک پروژه از آن استفاده کرد.
او نحوه ساخت http سرور در نود جی اس را آموزش داد و با معرفی ماژول nodemon، که برای ریلود کردن پروسس نود بکار گرفته میشود، کار با فریمورک express را آفاز کرد. به کمک فریمورک اکسپرس یک وبسایت چند صفحهای با روتهای هوشمند ایجاد کرد که بعد از آن، برای جذابتر کردن وبسایت، صفحه ای به آن اضافه کرد که کاربران بتوانند به صورت real-time در آن چت کنند. او برای پیاده سازی این صفحه از ماژول socket.io استفاده کرد.
در آخر او با اشاره به پروتکل webSocket و نحوه عملکرد سوکت ها در نود جی اس، چت روم پرسرعتی را به کمک آن، پیاده سازی کرد.
او به این نکته نیز اشاره کرد که برای پایداری چت ها میتوان از دیتابیسهایی مانند مونگو دی بی استفاده کرد و یا با کش کردن چت ها میتوان از دیتابیس ردیس (که یک دیتابیس درون مموری هست) استفاده کرد. او قبلا ماژول رایکش را دراین حوزه توسعه و در دسترس عموم گذاشته است. در نهایت با معرفی و استفاده از پروسس منیجر pm2 و کاربرد های آن ارائه خود را پایان داد.
جلسات باز نرمافزاری یک رویداد پرمحتواست که با استقبال خوبی مواجه شده است. این رویداد که بدون حامی مالی برگزار میشود، برای ادامه فعالیت خود نیاز به تامین مخارج دارد. هزینههایی از قبیل تهیه صندلی، تهیه پذیرایی درخور مهمانان و... است. این هزینه ها از طریق فروش بلیط های حمایتی رویداد تامین میشود. البته برگزاری این رویداد رایگان بوده و رایگان خواهد ماند. این فرصت را غنیمت دانستم تا در حمایت از برگزار کنندگان اشاره کوچکی به این موضوع کنم. خیالتان تخت که یک ریالی هم از برگزاری این رویداد به جیب برگزارکنندگان نمیرود. تمام تلاش برگزار کنندگان افزایش سطح دانش فعالان استارتاپی و علاقمندان این حوزه است.
The event Open Software Sessions fortunately continues to thrive. An event that is held twice a month and each session addresses a new topic. So far, Open Software Sessions have covered various topics such as Blockchain, Deep Learning, Customer Happiness, and UX Writing, React Native, and more. In the fifth session of the Open Software Sessions, which was attended by Raichat developers, the topic was dedicated to Node.js.
Saman Baboli, co-founder and CTO of Raichat, and Hamed Gholizadeh, co-founder and CIO of Raichat, were responsible for presenting materials on Node.js in this session. Saman Baboli began by introducing and explaining the concepts of Node.js, and then Hamed Gholizadeh demonstrated the practical use and capabilities of Node.js through a project.
Fifth session of Open Software Sessions in Tabriz: Raichat team talks about Node.js - Technology at the time of Tabriz
What is Node.js?
Node.js is neither a programming language nor a framework. Node.js is an environment that allows us to run JavaScript code on the server or machine. Node.js code is converted to machine language by Google’s V8 engine.
Why use Node.js?
Mainly, today Node.js is used in cases where there is a need to respond to a large volume of requests.
The reason for the widespread use of Node.js is its speed, which is made possible thanks to Non-Blocking I/O and its asynchronous nature. To better understand this, let’s consider a simple Async example. Suppose an employee in an office needs to obtain information from an inspector to complete a report. When this employee calls the inspector and requests the information, the inspector responds that he needs half an hour to deliver the information. The employee remains on hold until the information is received, and the phone is busy during the information retrieval period (sync), but if he asks the inspector for the information and wants to call back after it is ready, he can do other tasks during that half hour or take other calls, and after half an hour, the inspector can call back and provide the information to the employee. As you can see, nothing was blocked here (async).
One of the best features of Node.js is that you can use a single language called JavaScript on both the client (browser) and server sides. Therefore, you will need less time to learn. It is also excellent for launching MVPs because you can set up a version for all platforms with their help and spend much less time preparing them.
Another advantage of Node.js is the large and active ecosystem of developers who have provided a vast number of modules, frameworks, and tutorials for Node.js. All created modules are available on the npmjs site. npm stands for Node Package Manager.
Each project has a Package.json file, which is referred to as the "project ID card." This file contains general information about our project, such as name, version, description, etc., and also has a section that stores the names and versions of the modules used so that when the project is run by other team members or on another system, all modules can be installed by executing the following command.
$npm install
Next, Saman Baboli introduced several popular and well-known modules in npm, such as: express, hapi, socket.io, redis, pm2.
What is a template engine?
Template engines offer various features and usually make working with data on the view side easier for us. For example, the pug template engine simplifies the syntax of HTML code for us and also makes working with data easier. One of the notable features of these template engines is support for loops and if statements. To better understand, you can refer to the example in the slides.
Node.js frameworks
express: One of the most famous Node.js frameworks.
Koa: The express development team took charge of its development and focuses more on generators.
sails js: Based on the MVC architecture and similar to Ruby on Rails.
hapi: Another framework for Node.js that has a higher level compared to express and koa and has made building applications and services easier.
ES6 is the 2015 version of ECMAScript. ECMAScript is the name of a standard that JavaScript follows. Each new version of ECMAScript has new features, and so far, version ES8 has been released, but the latest version supported by Node.js is version ES6.
Difference between web socket and http
In communication via the http protocol, requests must be sent from the client to the server at regular intervals to receive new data if available. However, in communication via the web socket protocol, a permanent connection is established between the client and server that listens to events or can send events to each other. Refer to the image in the slides.
Do not use Node.js if
In applications that have high CPU consumption or, in other words, require a lot of computations, such as image processing. It is also better to use nginx for serving static files to users and not to delegate this task to Node.js.
Use Node.js in
Real-time apps like chat apps or dashboards that need to continuously update and display new data to the user are a good choice for Node.js. A chat application is a good example for Node.js because it has a high volume of data transfer and also a lot of traffic without needing high CPU consumption.
In the continuation of his presentation, Saman Baboli referred to the topic of load balancing in Node.js. In one sentence, load balancing can be defined as distributing incoming requests to multiple servers. To better understand this topic, you can read the article he wrote on Medium.
As a final topic, he mentioned companies that use Node.js, such as Uber, which is recognized as one of the first and largest users of Node.js, and more information can be found at this link. Other businesses like PayPal, LinkedIn, Yahoo, and several others are among these companies.
To download the slides of Saman Baboli's presentation, click here.
After Saman Baboli's presentation, Hamed Gholizadeh, the IT manager of Raichat, demonstrated the concepts of Node.js practically for the attendees.
Hamed Gholizadeh - Fifth session of Open Software Sessions
Gholizadeh began his presentation with an introduction to Node.js. He first demonstrated how Node.js and its modules work practically. Then he explained the applications of the ID card file (package.json) and created an ID card file. After that, he explained how to work with npm and, for example, installed the lodash module and used it in a project.
He taught how to create an http server in Node.js and started working with the express framework by introducing the nodemon module, which is used to reload the Node process. With the express framework, he created a multi-page website with smart routes, and then to make the website more attractive, he added a page where users could chat in real-time. He used the socket.io module to implement this page.
Finally, he referred to the webSocket protocol and how sockets work in Node.js, implementing a high-speed chat room with it.
He also mentioned that to maintain chat stability, databases like MongoDB can be used, or by caching chats, Redis (which is an in-memory database) can be utilized. He previously developed the Raichat module in this area and made it available to the public. Finally, he concluded his presentation by introducing and using the process manager pm2 and its applications.
Open Software Sessions is a content-rich event that has received good reception. This event, which is held without financial sponsorship, needs to cover its expenses to continue its activities. Expenses such as providing chairs, catering for guests, etc. These costs are covered through the sale of supportive tickets for the event. However, this event is free to attend and will remain free. I took this opportunity to briefly mention this topic in support of the organizers. Rest assured that not a single rial from this event goes into the pockets of the organizers. The organizers' entire effort is to increase the knowledge level of startup activists and enthusiasts in this field.
دیدگاهها Comments
هنوز دیدگاهی ثبت نشده. No comments yet.