<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: darryncampbell</title>
    <description>The latest articles on Forem by darryncampbell (@darryncampbell).</description>
    <link>https://forem.com/darryncampbell</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1022605%2F396c41de-2e8f-4307-91ab-f5d19cc51763.jpg</url>
      <title>Forem: darryncampbell</title>
      <link>https://forem.com/darryncampbell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/darryncampbell"/>
    <language>en</language>
    <item>
      <title>Long Polling vs WebSockets</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Tue, 28 Nov 2023 11:07:18 +0000</pubDate>
      <link>https://forem.com/pubnub/long-polling-vs-websockets-8fk</link>
      <guid>https://forem.com/pubnub/long-polling-vs-websockets-8fk</guid>
      <description>&lt;p&gt;Web applications were initially designed as a client-server architecture. Clients will create an &lt;a href="https://www.pubnub.com/guides/http/" rel="noopener noreferrer"&gt;HTTP/HTTPS&lt;/a&gt; request to the designated server requesting or modifying a piece of information. For example, a basic web application will follow a similar flow.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The client requests data from the server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The load balancer routes the requests to the appropriate server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server queries the applicable database for some data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The database returns the queried data to the server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server processes the data and sends the data back to the client&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F20KLOkp9uPZUiZtjp5VpWn%2Fe4e6f4e48f9f0129766611ead3da8fa6%2FHTTP_Polling.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F20KLOkp9uPZUiZtjp5VpWn%2Fe4e6f4e48f9f0129766611ead3da8fa6%2FHTTP_Polling.png" alt=" Client Server Communication"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A simple HTTP request is the most common way to receive server information. However, what if you wanted to get data back as soon as it was added to the database or sent to the server? With a basic web application designed as a client-server architecture, you would have to repeat this process over and over to check if there is new information that has been added to the database. This process is known as &lt;a href="https://en.wikipedia.org/wiki/Polling_(computer_science)" rel="noopener noreferrer"&gt;polling&lt;/a&gt; or sometimes referred to as short polling. The downside to this approach is that data will not be returned the majority of the time as the server hasn’t received any new information. Let's solve this problem and discuss the advantages and disadvantages of Long Polling and Websockets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview: Long polling vs WebSockets
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Long Polling
&lt;/h4&gt;

&lt;p&gt;Long polling is an &lt;a href="https://www.pubnub.com/blog/http-long-polling/" rel="noopener noreferrer"&gt;approach&lt;/a&gt; where the client will send an API request to the server, but instead of receiving an instant response from the server, it entails maintaining the HTTP connection. Maintaining the HTTP connection enables the server to reply later when data becomes available or the timeout threshold has been reached. After receiving the response, the client will immediately send the subsequent request.&lt;/p&gt;

&lt;p&gt;Instead of sending numerous requests repeatedly until the server receives new information, as in polling, the client only has to send one request to the server to get the latest information. After receiving the data, the client can initiate a new request, repeating this process as often as necessary.&lt;/p&gt;

&lt;p&gt;A flow for Long polling will look as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The client-side makes an HTTP request to the server requesting some data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server does not respond immediately with the requested information but waits until new information is available&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When new data becomes available, the server responds with new information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The client receives that data and immediately sends another request to the server, re-starting the process&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2FEB0j6VRCXSe28P5ruiSZ%2Fa149b8fdbbdb77e5f3bb81aab9902dac%2FHTTP_Long_Polling.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2FEB0j6VRCXSe28P5ruiSZ%2Fa149b8fdbbdb77e5f3bb81aab9902dac%2FHTTP_Long_Polling.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  WebSockets
&lt;/h4&gt;

&lt;p&gt;WebSockets are a modern &lt;a href="https://www.pubnub.com/learn/glossary/what-is-websocket/" rel="noopener noreferrer"&gt;technology&lt;/a&gt; built on top of a device’s &lt;a href="https://www.pubnub.com/guides/tcp-ip/" rel="noopener noreferrer"&gt;TCP/IP&lt;/a&gt; stack. The only relationship to HTTP protocol is that HTTP servers interpret its handshake to establish a connection. It is a bidirectional, &lt;a href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)" rel="noopener noreferrer"&gt;full-duplex&lt;/a&gt; protocol that is stateful, meaning the connection between the client and server will persist until either party decides to terminate it. &lt;/p&gt;

&lt;p&gt;Unlike long polling, which is only a half-duplex solution, the process does not need to repeat after receiving the latest information from the server. WebSocket technology allows us to keep the connection alive after the new information has been returned and perform bidirectional updates. The client can send information back to the server and listen for further information in the same request. &lt;/p&gt;

&lt;p&gt;A WebSocket connection flow will look something like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The client-side initiates a WebSocket by sending a request which contains an upgrade header to switch the communication protocol to a WebSocket protocol&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the server can establish a connection and agrees with the client's terms, then it sends a response to the client acknowledging the WebSocket handshake request&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the client receives a successful WebSocket connection, the client and the server can now start sending data in both directions allowing real-time communication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server or the client decides to terminate the connection&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7LukONcMxY3FxnW3B4xkYI%2F0ffd1c3ffbbbffc614155918de821e9c%2FWebSocket-Diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7LukONcMxY3FxnW3B4xkYI%2F0ffd1c3ffbbbffc614155918de821e9c%2FWebSocket-Diagram.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When to choose Long Polling vs WebSockets
&lt;/h2&gt;

&lt;p&gt;There is a debate on when to use long polling or WebSocket protocol. Both have their benefits and limitations and are often used for different purposes. In this section, we will discuss the key benefits of both long polling and WebSockets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros of Long Polling vs WebSockets
&lt;/h3&gt;

&lt;p&gt;Compatibility: Long polling is an older technology used more as a technique, making it a more compatible option than WebSockets. It is built on top of &lt;a href="https://en.wikipedia.org/wiki/XMLHttpRequest" rel="noopener noreferrer"&gt;XMLHttpRequest&lt;/a&gt;, consistent with a broader range of web browsers and network configurations.&lt;/p&gt;

&lt;p&gt;Network: With today’s technology, people constantly switch networks from 3G to LTE to WiFi. WebSockets must be configured to adapt to a change in the network connection. This configuration is because the connection has to be re-established with the server and cannot be revived after the client has opted to close the connection. With long polling, this is not an issue as it is set up where after the predetermined time (usually 20 seconds), the client will try to send another request re-establishing a connection with the server automatically and does not have to be handled in an error state as with WebSockets. &lt;/p&gt;

&lt;h3&gt;
  
  
  Use cases to choose long polling over WebSockets
&lt;/h3&gt;

&lt;p&gt;Long polling and WebSockets are generally used in cases where real-time updates are required. Some examples include in-app chat, real-time pricing, geo-tracking, and IoT.  &lt;/p&gt;

&lt;p&gt;Long polling provides benefits over WebSockets in use cases with low-frequency real-time updates. These benefits are because long polling is a half-real-time solution where the connection needs to be re-established. Additionally, as mentioned above, if users are in an environment with low bandwidth or an unstable network provider, long polling is architected to re-establish the connection with no additional complications. However, long polling is an older technology/technique to perform real-time updates. As a result, it is less advanced and less flexible than WebSockets but has more support for legacy systems. &lt;/p&gt;

&lt;h2&gt;
  
  
  When to choose WebSockets vs long polling
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pros of WebSockets vs long polling
&lt;/h3&gt;

&lt;p&gt;Reduced Resource Utilization: WebSockets maintain a persistent connection between the client and the server, reducing the overhead of establishing a new connection for each real-time update. The constant connection reduces resource utilization on the client and server side regarding network bandwidth, memory, and CPU to achieve real-time communication.&lt;/p&gt;

&lt;p&gt;Improved Scalability: Due to the nature of WebSockets and its bidirectional communication between the client and the server, the server can push updates to the client in real time, reducing the number of requests sent. Long polling must re-establish a connection every time the client needs new information. As the user base scales, this can put a lot of strain on an individual server.&lt;/p&gt;

&lt;p&gt;Advanced Functionality: WebSockets provide full-duplex communication channels that achieve real-time data transfer and low latency. Long polling is sometimes considered only a half-real-time solution and not ideal for high-traffic scenarios or use cases that require real-time updates. Advanced functionality brings a smoother end-user experience as they will receive more seamless updates to their application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use cases to choose WebSockets over long polling
&lt;/h3&gt;

&lt;p&gt;WebSockets are better suited for applications that require high-frequency updates. Examples include &lt;a href="https://www.pubnub.com/solutions/chat/" rel="noopener noreferrer"&gt;chat applications&lt;/a&gt; or &lt;a href="https://www.pubnub.com/solutions/data-streaming/" rel="noopener noreferrer"&gt;real-time data feeds&lt;/a&gt;. The persistent connection allows for efficient transmission making it a more seamless experience for the end user. Multiplayer games and collaboration tools generally use WebSockets as well. The bi-directional two-way communication allows the server to signal the client, which can be beneficial for receiving real-time updates from other clients. For example, the server can signal the client, telling it to update another player's position depending on their actions.&lt;/p&gt;

&lt;p&gt;In terms of scaling, or when a user base starts scaling, it is ideal to switch to the WebSocket protocol. The strain on an individual server will become too large if the client base uses long polling technology. Sending a request every 20 seconds has poor utilization and will cause the server to slow down over time, increasing &lt;a href="https://www.pubnub.com/blog/latency-definition/" rel="noopener noreferrer"&gt;latency&lt;/a&gt; per request.&lt;/p&gt;

&lt;h2&gt;
  
  
  How PubNub fits into the conversation of Long polling vs WebSockets
&lt;/h2&gt;

&lt;p&gt;WebSockets and long polling do offer valuable solutions to create real-time applications. However, many other considerations come into play when building upon these use cases. With today’s technology, nothing is as simple as sending a message or data from one client to another. There is almost always required functionality on top of the real-time system that a developer is trying to create. For example, in an in-app chat, you can look at presence updates (signalling) when users are online, profanity filtering, or even read/deliver messages. &lt;/p&gt;

&lt;p&gt;On top of adding specific functionality, there are still problems with underlying infrastructure, such as complexities with handling scalability. When using particular technologies such as &lt;a href="https://www.pubnub.com/learn/glossary/what-is-socketio/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt; to create a WebSocket, developers will still have to take care of dynamically spawning servers around the world behind a load balancer to manage the utilization of each server. These problems become more and more complex the more you scale.&lt;/p&gt;

&lt;p&gt;PubNub’s &lt;a href="https://www.pubnub.com/learn/glossary/what-is-real-time-data/" rel="noopener noreferrer"&gt;real-time data APIs&lt;/a&gt; allow users to develop powerful, &lt;a href="https://www.pubnub.com/guides/event-driven-architecture/" rel="noopener noreferrer"&gt;event-driven applications&lt;/a&gt; to facilitate real-time communication across all devices, regardless of the specific use case. PubNub offers a variety of SDKs, such as a &lt;a href="https://www.pubnub.com/docs/sdks/javascript" rel="noopener noreferrer"&gt;JavaScript SDK&lt;/a&gt; for web applications and a &lt;a href="https://www.pubnub.com/docs/sdks/c-core" rel="noopener noreferrer"&gt;C-Core SDK&lt;/a&gt; for IoT applications, to ensure seamless integration with the chosen device. With PubNub, you don’t have to worry about selecting a suitable alternative or the underlying complexities of implementing a real-time solution. &lt;/p&gt;

&lt;p&gt;Now that you understand when to use long polling vs WebSockets &lt;a href="https://admin.pubnub.com/#/register" rel="noopener noreferrer"&gt;sign up for a free trial&lt;/a&gt; or &lt;a href="https://www.pubnub.com/company/contact-sales/" rel="noopener noreferrer"&gt;schedule a demo&lt;/a&gt; to explore what you can build with PubNub.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://www.pubnub.com/blog/long-polling-vs-websockets/" rel="noopener noreferrer"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/" rel="noopener noreferrer"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/" rel="noopener noreferrer"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>WebSocket Alternatives</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Tue, 28 Nov 2023 11:02:02 +0000</pubDate>
      <link>https://forem.com/pubnub/websocket-alternatives-4gdj</link>
      <guid>https://forem.com/pubnub/websocket-alternatives-4gdj</guid>
      <description>&lt;p&gt;The rapid growth of the internet has led to an increase in demand for real-time technology, enhancing communication between clients and servers. WebSockets have grown in popularity and are now one of the most popular methods for enabling real-time communication. However, WebSockets may not always be the best option, as the choice of technology depends on the specific use case. There are many alternative options available that might be better suited for particular situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a WebSocket?
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://www.pubnub.com/guides/what-are-websockets-and-when-should-you-use-them/" rel="noopener noreferrer"&gt;WebSocket&lt;/a&gt; is a protocol that provides &lt;a href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)" rel="noopener noreferrer"&gt;full-duplex&lt;/a&gt; communication and bi-directional communication channels between clients and servers over the web. It allows real-time data transmission between the client side and the client-server without reconnecting to the server. Unlike a traditional &lt;a href="https://www.pubnub.com/guides/http/" rel="noopener noreferrer"&gt;HTTP protocol&lt;/a&gt;, WebSockets enable the client and server to initiate communication and send data anytime, making them ideal for realtime data transmission.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7LukONcMxY3FxnW3B4xkYI%2F0ffd1c3ffbbbffc614155918de821e9c%2FWebSocket-Diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7LukONcMxY3FxnW3B4xkYI%2F0ffd1c3ffbbbffc614155918de821e9c%2FWebSocket-Diagram.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why choose a WebSocket Alternative?
&lt;/h2&gt;

&lt;p&gt;Almost every time developers think about real-time communication, we use WebSockets. Why is this the case? Are they the only/best choice to achieve real-time transmission, no matter the use case? &lt;/p&gt;

&lt;p&gt;To put it simply, WebSockets provide advanced functionalities but, depending on the use case, can have many drawbacks that can quickly increase a project's complexity. To solve this, let's consider why developers would pick a WebSocket alternative.&lt;/p&gt;

&lt;p&gt;Performance: WebSockets are most known for their advanced functionality but sometimes can pose an overkill solution and lead to poor performance. It is essential to consider the use case when deciding to use WebSockets. For example, there are better solutions than WebSockets if you need a high concurrency of connections. WebSocket connections have to remain alive between the client and the server leading to increased load on the server and underlying infrastructure as you scale.&lt;/p&gt;

&lt;p&gt;Interoperability: WebSockets are supported by all modern web browsers/networks. By chance, if the software is being used in a legacy browser or network, it is essential to consider if WebSockets has support in that legacy system. Additionally, networks and proxies sometimes block WebSocket connections altogether, so where you use the software is another factor to consider.&lt;/p&gt;

&lt;p&gt;Security: WebSockets are not secure as some alternatives and have vulnerabilities that malicious actors can exploit. Some reasons WebSockets are considered less secure than other protocols include cross-site WebSocket hijacking and origin spoofing.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebSocket Alternatives
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Long Polling
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/long-polling/" rel="noopener noreferrer"&gt;Long polling&lt;/a&gt; is an approach where the client sends an HTTP/HTTPS request to the server, but instead of receiving an instant response from the server, it entails maintaining the HTTP connection. Maintaining the HTTP connection enables the server to reply later when data becomes available, or the timeout threshold has been reached. After receiving the response, the client will immediately send the subsequent request.&lt;/p&gt;

&lt;p&gt;Long polling achieves real-time communication without the bi-directional communication channels that WebSockets provide. The core difference is instead of maintaining a persistent connection as with WebSockets, the client will send multiple requests to re-establish the connection with the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2FEB0j6VRCXSe28P5ruiSZ%2Fa149b8fdbbdb77e5f3bb81aab9902dac%2FHTTP_Long_Polling.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2FEB0j6VRCXSe28P5ruiSZ%2Fa149b8fdbbdb77e5f3bb81aab9902dac%2FHTTP_Long_Polling.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  MQTT
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/mqtt/" rel="noopener noreferrer"&gt;MQTT&lt;/a&gt; is a lightweight, &lt;a href="https://www.pubnub.com/guides/everything-you-need-to-know-about-pub-sub/" rel="noopener noreferrer"&gt;publish-subscribe&lt;/a&gt;, machine-to-machine network protocol designed for remote locations with devices that have limited network bandwidth. Smart sensors, wearables, and other IoT devices typically need to use such a protocol due to their resource constraints. While WebSockets can be used for various applications, MQTT is explicitly designed for machine-to-machine communication, which is why it’s considered an alternative under these use cases. MQTT provides features like low overhead, efficient message delivery, and support for offline operation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2R4S3y5yDtT3r91DvpbI8t%2F2df1e001799058ec32cadd3a887a8fbd%2FMQTT_Glossary_Broker_copy_4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F2R4S3y5yDtT3r91DvpbI8t%2F2df1e001799058ec32cadd3a887a8fbd%2FMQTT_Glossary_Broker_copy_4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  WebRTC
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/webrtc/" rel="noopener noreferrer"&gt;WebRTC&lt;/a&gt; is a free, open-source project written in &lt;a href="https://www.pubnub.com/docs/sdks/posix-cpp" rel="noopener noreferrer"&gt;C++&lt;/a&gt; and &lt;a href="https://www.pubnub.com/docs/sdks/javascript" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt; that provides web browsers and mobile devices with real-time communication. WebRTC is &lt;a href="https://dolby.io/blog/adding-pubnub-in-app-chat-to-your-webrtc-live-stream-app/" rel="noopener noreferrer"&gt;built&lt;/a&gt; to stream audio and video directly from peer-to-peer without needing server-side connections or additional plugins. For use cases such as live streaming, video conferences, or group calls, these functionalities can be engineered rapidly with HTML5 and &lt;a href="https://www.pubnub.com/guides/javascript/" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt; using the WebRTC API. In summary, WebRTC’s peer-to-peer nature can help reduce the server load and provide a more efficient and high-quality communication experience compared to WebSockets in these use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  WebTransport vs WebSocket
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/webtransport/" rel="noopener noreferrer"&gt;WebTransport&lt;/a&gt; is a new web communication protocol that provides a low-latency and high-throughput connection between a client and a server. It was created to address some of the limitations of traditional protocols, such as WebSockets, and provide a modern alternative that can better handle the demands of today’s web applications. &lt;/p&gt;

&lt;p&gt;While WebSockets typically start as an HTTP/1.1 Upgrade request, WebTransport supports multiple protocols, including HTTP/2, &lt;a href="https://en.wikipedia.org/wiki/HTTP/3" rel="noopener noreferrer"&gt;HTTP/3&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/QUIC" rel="noopener noreferrer"&gt;QUIC&lt;/a&gt;, and is a &lt;a href="https://www.pubnub.com/guides/udp/" rel="noopener noreferrer"&gt;UDP&lt;/a&gt;-based transport. Additionally, WebTransport supports sending data reliably via its streams API and unreliably via its datagrams API. With this new technology, how does it compare to WebSockets and what are the advantages of switching to WebTransport protocol? &lt;/p&gt;

&lt;p&gt;Advanced Functionality: A challenge with WebSockets is that it can be difficult to send different types of data, such as an image and text, over a single connection. The issue arises because the data is divided into smaller units, known as packets, for transmission over a &lt;a href="https://www.pubnub.com/guides/tcp-ip/" rel="noopener noreferrer"&gt;TCP&lt;/a&gt; connection. When dividing the data into packets, there is no way to distinguish between the plain text and the encoded image contained within the data. To resolve this, WebSockets often need to make two separate connections between the server and client, one for pictures and one for text. However, WebTransport provides a solution by supporting multiple streams, known as multiplexing. By supporting multiple streams, developers can send different types of data over one connection, reducing the overhead of establishing and maintaining numerous connections. This becomes essential when the server needs to support many clients. Multiple connections per client, as in WebSockets, can lead to complexities when the server starts to scale.&lt;/p&gt;

&lt;p&gt;Improved Performance: WebTransport is a UDP-based transport protocol more performant than TCP-based protocols like WebSockets. Due to it being UDP based, there is no error correction or retransmission. Error correction refers to detecting and correcting an error that may occur in the transmitted data. Retransmission refers to the processing of resending lost or corrupted data packets over the network. Due to not having the overhead of correction and retransmission, this lowers the &lt;a href="https://www.pubnub.com/guides/whats-so-important-about-low-latency/" rel="noopener noreferrer"&gt;latency&lt;/a&gt; of WebTransport. WebTransport also offers a WebTransportCongestionControl configuration which can optimize the throughput and latency of requests. Secondly, Establishing a new connection with WebTransport over QUIC handshake is typically faster than TCP over TLS.&lt;/p&gt;

&lt;p&gt;Reliability: The lack of error correction and retransmission might make a developer think WebTransport is less reliable than WebSockets. That is not the case; WebTransport, by design, provides more reliability than WebSockets, especially in scenarios with high-bandwidth communication and challenging network conditions. WebTransport &lt;a href="https://www.w3.org/TR/webtransport/" rel="noopener noreferrer"&gt;provides&lt;/a&gt; a streams API for reliable unidirectional (WebTransportReceiveStreams) or bidirectional two-way (WebTransportBidirectionalStream) data transfer. This API endpoint offers configurations such as WebTransportReliabilityMode, which can allow the connection to have a TCP fallback in scenarios where reliable data transfer is essential.&lt;/p&gt;

&lt;p&gt;Security: WebTransport offers a more secure protocol than WebSockets. It provides end-to-end encryption by default, which ensures that the data between the client and server cannot be intercepted or modified in transit. One of these security features is the use of an “Origin” header, which provides a way for the server to verify that the requests are coming from a trusted source. This helps prevent cross-site request forgery (CSRF) attacks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Alternatives
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Sever-Sent Events
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/server-sent-events/" rel="noopener noreferrer"&gt;Server-Sent Events (SSE)&lt;/a&gt; is a server push technology that allows a server to push data to a client over a single HTTP connection. SSE is a simple, efficient protocol for sending real-time updates to a web page or application, making it well-suited for specific use cases. Unlike traditional HTTP requests, which close the connection after a single response, SSE opens a long-lived connection between the server and the client, allowing the server to send multiple updates over the same connection. Compared to WebSockets’s bidirectional connections, Server-Sent Events are only unidirectional. The client only has to initiate the connection and close it. However, the limited feature set of Server-Sent Events makes it easier to implement and debug. Server-Sent Events was the start of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource" rel="noopener noreferrer"&gt;EventSource&lt;/a&gt; API, which makes it easy to implement in web browsers. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7hGVCgK5DEFPKeakTFWKxu%2F7462285d9e144607b97f93a58d82a609%2FServer-Sent.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2F7hGVCgK5DEFPKeakTFWKxu%2F7462285d9e144607b97f93a58d82a609%2FServer-Sent.png" alt="Server Sent Events exchange"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How PubNub can help with WebSocket alternatives
&lt;/h2&gt;

&lt;p&gt;WebSocket protocol and its alternatives, such as WebTransport, are all valuable solutions to handling real-time communications. However, no matter what solution is selected, time will be wasted in implementing the underlying infrastructure and complexities when scaling. For example, when using particular technologies such as &lt;a href="https://www.pubnub.com/guides/what-is-socket-io/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt; to create a WebSocket server, developers will still have to take care of dynamically spawning servers around the world behind a load balancer to manage the utilization of each web server.&lt;/p&gt;

&lt;p&gt;PubNub’s &lt;a href="https://www.pubnub.com/learn/glossary/what-is-real-time-data/" rel="noopener noreferrer"&gt;real-time data APIs&lt;/a&gt; allow users to develop powerful, event-driven applications to facilitate real-time communication across all devices, regardless of the specific use case. PubNub offers a variety of SDKs, such as a &lt;a href="https://www.pubnub.com/docs/sdks/javascript" rel="noopener noreferrer"&gt;JavaScript SDK&lt;/a&gt; for web applications and a &lt;a href="https://www.pubnub.com/docs/sdks/c-core" rel="noopener noreferrer"&gt;C-Core SDK&lt;/a&gt; for IoT applications, to ensure seamless integration with the chosen device. With PubNub, you don’t have to worry about selecting a suitable alternative or the underlying complexities of implementing a real-time solution. &lt;/p&gt;

&lt;p&gt;Now that you understand when to use WebSockets or an alternative, &lt;a href="https://admin.pubnub.com/#/register" rel="noopener noreferrer"&gt;sign up for a free trial&lt;/a&gt; or &lt;a href="https://www.pubnub.com/company/contact-sales/" rel="noopener noreferrer"&gt;schedule a demo&lt;/a&gt; to explore what you can build with PubNub.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://www.pubnub.com/blog/websockets-alternatives/" rel="noopener noreferrer"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/" rel="noopener noreferrer"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/" rel="noopener noreferrer"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is Geohashing?</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Mon, 27 Nov 2023 16:03:38 +0000</pubDate>
      <link>https://forem.com/pubnub/what-is-geohashing-52pg</link>
      <guid>https://forem.com/pubnub/what-is-geohashing-52pg</guid>
      <description>&lt;p&gt;Geohashing (or geohash) is a &lt;a href="https://www.pubnub.com/learn/glossary/what-is-geocoding-and-reverse-geocoding/" rel="noopener noreferrer"&gt;geocoding&lt;/a&gt; method used to encode geographic coordinates (latitude and longitude) into a short string of digits and letters delineating an area on a map, which is called a cell, with varying resolutions. The more characters in the string, the more precise the location.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an example of a geohash?
&lt;/h3&gt;

&lt;p&gt;Geohash is a public domain of encoding coordinates. An example of a geohash is the coordinate pair 28.6132,77.2291 being converted into a geohash of ttnfv2u. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is the maximum length of a geohash?
&lt;/h3&gt;

&lt;p&gt;The maximum length of a geohash is 12.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Geohashing Work?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Geohash algorithm and calculation
&lt;/h3&gt;

&lt;p&gt;Geohashes use Base-32 alphabet encoding (characters can be 0 to 9 and A to Z, excl "A", "I", "L" and "O”). Imagine the world is divided into a grid with 32 cells. The first character in a geohash identifies the initial location as one of the 32 cells. This cell will also contain 32 cells, and each one of these will contain 32 cells (and so on repeatedly). Adding characters to the geohash sub-divides a cell, effectively zooming in to a more detailed area.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2Fassetglossary-17su9wok1ui0z7r%2F55420735eef7b0469e22092fdc0683f4%2Fgeohashing-large-scale-example.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2Fassetglossary-17su9wok1ui0z7r%2F55420735eef7b0469e22092fdc0683f4%2Fgeohashing-large-scale-example.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The precision factor determines the size of the cell. For instance, a precision factor of one creates a cell 5,000km high and 5,000km wide, a precision factor of six creates a cell 0.61km high and 1.22km wide, and a precision factor of nine creates a cell 4.77m high and 4.77m wide (cells are not always square).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2Fassetglossary-17su9wok1ui0z7s%2F54cff1f40a461b1019b02cf1c35b542a%2Fgeohashing-examples-san-francisco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.ctfassets.net%2F3prze68gbwl1%2Fassetglossary-17su9wok1ui0z7s%2F54cff1f40a461b1019b02cf1c35b542a%2Fgeohashing-examples-san-francisco.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Geohashing Examples and Use Cases
&lt;/h2&gt;

&lt;p&gt;Geohashing was originally developed as a &lt;a href="https://bitly.com/" rel="noopener noreferrer"&gt;URL-shortening service&lt;/a&gt; but it is now commonly used for spatial indexing (or spatial binning), location searching, mashups and creating unique place identifiers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Geohash benefits
&lt;/h3&gt;

&lt;p&gt;A geohash is shorter than a regular address, or latitude and longitude coordinates, and therefore easier to share, remember and store.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Social Networking:&lt;/strong&gt; Chat with people near you within a particular cell, and to &lt;a href="https://www.pubnub.com/learn/glossary/what-is-in-app-chat/" rel="noopener noreferrer"&gt;create chat apps&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Proximity Searches:&lt;/strong&gt; Find nearby locations using &lt;a href="https://www.pubnub.com/learn/glossary/what-is-a-map-api/" rel="noopener noreferrer"&gt;API mapping&lt;/a&gt; or &lt;a href="https://www.pubnub.com/learn/glossary/what-is-a-geolocation-api/" rel="noopener noreferrer"&gt;geolocation APIs&lt;/a&gt; and identify places of interest, restaurants, shops and accommodation establishments in an area.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Digital Travels:&lt;/strong&gt; Geohashers go on global expeditions to meet people and explore new places. The twist: the destination is a computer-generated geohash and participants in this turnkey travel experience have to write up and post their story on the internet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Interactive Apps:&lt;/strong&gt; Geohashing can be used to create &lt;a href="https://www.pubnub.com/blog/connected-shared-experiences-a-developers-guide/" rel="noopener noreferrer"&gt;real-time, interactive apps&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can try it out &lt;a href="https://www.movable-type.co.uk/scripts/geohash.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Use the coordinates 40.748440989 and -73.985663981 for a view of the area around the Empire State Building in New York. Change the precision factor to increase or decrease the resolution (size of the cell), or use the geohash dr5ru6j2c5fqt for a 13-digit precision factor to zoom right in.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the drawbacks to using a geohash?
&lt;/h3&gt;

&lt;p&gt;The drawbacks to geohashing include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using a grid based geohashing algorithm does need meet high-precision requirements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Geohashing deviation changes as latitude increases because the Earth is an irregular ellipse.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ready to incorporate real-time functionality in your app? &lt;a href="https://admin.pubnub.com/#/register" rel="noopener noreferrer"&gt;Create a free PubNub account today&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/learn/glossary/what-is-a-geolocation-api/" rel="noopener noreferrer"&gt;Geolocation and Geotracking&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/blog/realtime-geo-tracking-app-react-native/" rel="noopener noreferrer"&gt;React Native Geolocations Tracking App&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://pubnub.com/guides/what-is-geohashing/" rel="noopener noreferrer"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/" rel="noopener noreferrer"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/" rel="noopener noreferrer"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs" rel="noopener noreferrer"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is HTTP Streaming?</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Mon, 27 Nov 2023 09:09:44 +0000</pubDate>
      <link>https://forem.com/pubnub/what-is-http-streaming-2ihb</link>
      <guid>https://forem.com/pubnub/what-is-http-streaming-2ihb</guid>
      <description>&lt;h2&gt;
  
  
  What is HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming, also known as HTTP-based streaming or HTTP live streaming, is a technique used to deliver real-time multimedia content, such as audio or video, over the Internet. This protocol allows continuous data transmission from a server to a client device, enabling users to consume media content without requiring complete file downloads.&lt;/p&gt;

&lt;p&gt;Unlike traditional file download methods, where the entire file needs to be downloaded before playback can begin, HTTP streaming allows for the immediate playback of media content. It works by dividing the media file into smaller segments, or chunks, which are then continuously delivered to the client device.&lt;/p&gt;

&lt;p&gt;HTTP streaming utilizes the &lt;a href="https://www.pubnub.com/guides/http/"&gt;Hypertext Transfer Protocol (HTTP)&lt;/a&gt; as its communication protocol. It leverages existing web infrastructure and uses HTTP servers' scalability, caching, and &lt;a href="https://www.pubnub.com/guides/load-balancing/"&gt;load-balancing&lt;/a&gt; capabilities. This makes it an efficient and flexible solution for delivering real-time content to many users.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does HTTP streaming work?
&lt;/h2&gt;

&lt;p&gt;At a high level, HTTP streaming works by dividing the media file into small chunks and transmitting it to the client over an HTTP connection. The client, typically a web browser or a media player, continuously requests and receives these chunks, allowing for the seamless playback of the media.&lt;/p&gt;

&lt;p&gt;There are two main approaches to HTTP streaming: progressive download and adaptive streaming.&lt;/p&gt;

&lt;p&gt;1. &lt;a href="https://www.nginx.com/resources/glossary/progressive-download/"&gt;Progressive Download&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;Progressive Download lacks the adaptability that adaptive streaming provides. The entire media file is downloaded with progressive download before the client can start playing it. This means that if there are any Wi-Fi network interruptions or fluctuations in bandwidth during the download process, the user may experience buffering or delays in playback. This can frustrate users and may result in a poor user experience.&lt;/p&gt;

&lt;p&gt;2. &lt;a href="https://www.cloudflare.com/learning/video/what-is-adaptive-bitrate-streaming/#:~:text=Adaptive%20bitrate%20streaming%20adjusts%20video,%2C%20location%2C%20or%20Internet%20speed."&gt;Adaptive Streaming&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;Adaptive streaming is a crucial technology for developers building real-time chat and messaging applications that deliver media files like video or audio. It ensures that the media content can be streamed smoothly and efficiently, regardless of the user's network conditions. This is especially important in today's digital landscape, where users expect high-quality, uninterrupted media playback but sometimes have different internet speeds.&lt;/p&gt;

&lt;p&gt;Adaptive streaming typically involves the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Content-Encoding: The media file is encoded into multiple variations with different bitrates and quality levels. These variations are stored on the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Manifest File: A manifest file is created, which contains information about the available variations and their corresponding URLs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initial Request: The client requests the manifest file from the server, which provides information about the available variations of the media file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variant Selection: The client selects the desired variant based on network conditions and device capabilities. It then requests the corresponding media chunks from the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chunk Delivery: The server delivers the chunked media to the client over an HTTP connection. The client continuously requests and receives these chunks, adjusting the playback quality if necessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bitrate Adaptation: During playback, the client monitors the network conditions and dynamically adjusts the selected variant based on available bandwidth. It may switch to a lower bitrate variant if the network becomes congested or to a higher bitrate variant if the network conditions improve.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Seamless Playback: By continuously receiving and playing the media chunks, adaptive streaming provides a seamless playback experience, allowing users to enjoy the content without interruptions or buffering.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What benefits does HTTP streaming provide?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming provides several benefits for developers building real-time chat and messaging applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/blog/five-creative-ways-to-use-pubnub-data-stream/"&gt;Real-time data delivery&lt;/a&gt;: HTTP streaming enables real-time delivery of data, allowing messages to be instantly sent and received by users. This ensures that users can have real-time conversations without any noticeable delays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability: HTTP streaming is highly scalable, able to handle many concurrent connections and deliver messages to multiple users in real time. This is essential for applications that support a growing user base and handle high message volumes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduced network overhead: With HTTP streaming, only the necessary data is sent over the network when new information is available, reducing network overhead. This contrasts with other approaches like polling, where constant requests are made even when no new data exists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Efficient resource utilization: HTTP streaming allows for efficient utilization of server resources as it eliminates the need for frequent polling requests. This can reduce server load and improve performance, especially in applications with many active users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better user experience: HTTP streaming enhances the user experience for chat and messaging applications by providing real-time updates and instant message delivery. Users can have more interactive and engaging conversations without the frustration of delays or missed messages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security: HTTP streaming can provide a secure communication channel by leveraging existing security measures like HTTPS. This ensures that sensitive user data and conversations are protected from unauthorized access and eavesdropping.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are the drawbacks of using HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;There are several drawbacks to using HTTP streaming for real-time chat and messaging applications:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Latency:&lt;/strong&gt; HTTP streaming relies on a continuous connection between the client and the server. This can introduce &lt;a href="https://www.pubnub.com/guides/whats-so-important-about-low-latency/"&gt;latency&lt;/a&gt; as the server needs to maintain an open connection and send data in chunks. As a result, there might be a delay in delivering real-time messages to the users, which can impact the user experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; HTTP streaming can be resource-intensive for the client and the server. Maintaining a large number of open connections can put a strain on the server and limit its scalability. Additionally, clients need to handle and process incoming data streams, which can also be demanding on their resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compatibility:&lt;/strong&gt; All devices or networks may not support HTTP streaming. Some firewalls or proxies may block or interfere with the streaming connection, leading to communication issues. This can restrict the availability of the chat application to a subset of users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reliability:&lt;/strong&gt; Since HTTP streaming relies on a long-lived connection, interruptions or network failures can disrupt the streaming process. If the connection is lost, the client may need to reestablish it, potentially resulting in message loss or duplication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt; HTTP streaming does not inherently provide encryption or security measures for data transmission. Without additional layers of security, sensitive information exchanged through the chat application may be vulnerable to eavesdropping or unauthorized access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Battery Consumption:&lt;/strong&gt; Continuous connections and data streaming can quickly drain a mobile device's battery. This can be a concern for users of real-time chat applications, especially when using these applications for extended periods.&lt;/p&gt;

&lt;p&gt;Developers need to consider these drawbacks when choosing a technology for real-time chat and messaging applications. While HTTP streaming provides some benefits, such as leveraging existing security measures, developers should weigh these advantages against the potential drawbacks and assess whether they align with their specific use case and requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are some alternatives to HTTP Streaming?
&lt;/h2&gt;

&lt;p&gt;Some alternatives to HTTP streaming include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/what-are-websockets-and-when-should-you-use-them/"&gt;WebSockets&lt;/a&gt;: WebSockets is a communication protocol that provides full-duplex communication channels over a single &lt;a href="https://www.pubnub.com/guides/tcp-ip/"&gt;TCP&lt;/a&gt; connection. It allows for real-time, two-way communication between the client and the server, making it suitable for applications that require constant and low-latency data updates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/webrtc/"&gt;WebRTC&lt;/a&gt;: WebRTC (Web Real-Time Communication) is an open-source project that enables real-time communication between browsers and mobile applications. It provides APIs for voice and video calling and peer-to-peer data sharing, making it a popular choice for video conferencing and live streaming applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/mqtt/"&gt;MQTT&lt;/a&gt; (Message Queuing Telemetry Transport): MQTT is a lightweight messaging protocol designed for the Internet of Things (IoT). It is optimized for low-bandwidth and unreliable networks, making it suitable for IoT devices with limited resources. MQTT allows for efficient and real-time communication between IoT devices and backend systems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol"&gt;RTMP&lt;/a&gt; (Real-Time Messaging Protocol): RTMP is a streaming protocol developed by Adobe Systems for delivering audio, video, and data over the internet. It has been widely used for live streaming and video-on-demand applications, but its usage has decreased in recent years due to the rise of HTTP-based streaming protocols.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/streaming/"&gt;HLS&lt;/a&gt; (HTTP Live Streaming): HLS is an adaptive streaming protocol developed by Apple for delivering media content over the internet. It breaks the content into small, segmented files the client can download and playback in real time. HLS is widely used for streaming live events on-demand video, providing high-quality video playback on different devices and network conditions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SPDY (pronounced "speedy"): SPDY is a deprecated networking protocol developed by Google to improve the speed and security of web browsing. It aimed to reduce latency and optimize web content delivery by introducing features such as multiplexing, header compression, and prioritization of requests. However, SPDY has been superseded by HTTP/2, which incorporates many of its features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.websocketpp.org/"&gt;WebSocket++&lt;/a&gt;, &lt;a href="https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio.html"&gt;Boost.Asio&lt;/a&gt; and other libraries: These libraries and frameworks provide low-level APIs and tools for building real-time communication applications using protocols like WebSocket. They offer more flexibility and customization options than higher-level protocols like HTTP streaming but require more development effort and expertise.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to consider your application's specific requirements and constraints when choosing an alternative to HTTP streaming. Scalability, security, compatibility, and developer familiarity should be considered to ensure the best fit for your use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between TCP and HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol"&gt;TCP&lt;/a&gt; (Transmission Control Protocol) and HTTP (Hypertext Transfer Protocol) streaming are popular protocols for transmitting data over the internet. While TCP is a reliable, connection-oriented protocol, HTTP streaming is a more recent approach for streaming media content. Let's dive into the differences between these two:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connection-oriented vs. Connectionless:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP is a &lt;a href="https://nordvpn.com/cybersecurity/glossary/connection-oriented-protocol/#:~:text=A%20connection%2Doriented%20protocol%20is,data%20in%20a%20structured%20way."&gt;connection-oriented protocol&lt;/a&gt;, which means that it establishes a direct, reliable, and persistent connection between the sender and receiver. This ensures that data packets are delivered in the order they were sent and without any loss or duplication. HTTP streaming is based on a &lt;a href="https://avinetworks.com/glossary/connectionless-protocol/"&gt;connectionless model&lt;/a&gt;, where the client sends individual HTTP requests to the server, and the server responds with data chunks, typically in real time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Delivery:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP ensures the reliable delivery of data by using various mechanisms like error detection, retransmission of lost packets, and flow control. It guarantees that the receiver gets all the data in the same order it was sent. HTTP streaming focuses on real-time multimedia content delivery, such as audio or video. It prioritizes low latency and responsiveness over guaranteed delivery of every data packet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Port and Protocol:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP uses port numbers to identify applications or services running on a device. The default port for TCP is 80 for HTTP communication. On the other hand, HTTP streaming typically uses higher-level protocols like HTTP Live Streaming (HLS) or &lt;a href="https://en.wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP"&gt;Dynamic Adaptive Streaming over HTTP (DASH)&lt;/a&gt; for media content delivery. These protocols operate over standard HTTP ports (80 or 443).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP is designed for point-to-point communication between two devices. It may face scalability challenges when trying to handle many simultaneous connections. HTTP streaming can leverage load balancing techniques and content delivery networks (CDNs) to distribute the streaming workload across multiple servers, enabling scalability for handling high traffic volumes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TCP provides inherent security features such as encryption and authentication. However, additional security measures like &lt;a href="https://www.pubnub.com/guides/ssl-tls/"&gt;SSL/TLS&lt;/a&gt; can be implemented on top of TCP to ensure secure communication. HTTP streaming can also use SSL/TLS for secure data transmission. Additionally, content protection technologies like digital rights management (DRM) can be applied to protect multimedia content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compatibility:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All major operating systems, networking equipment, and programming languages widely support TCP. It is a foundational protocol for Internet communication. HTTP, as an application-layer protocol, is also widely supported, and HTTP streaming can be implemented on top of existing HTTP infrastructure.&lt;/p&gt;

&lt;p&gt;In conclusion, while TCP is a reliable and connection-oriented protocol suitable for general data transmission, HTTP streaming is a recent approach focused on real-time multimedia content delivery. Each protocol has its strengths and weaknesses and is used in different scenarios. Developers building real-time chat and messaging applications can choose between TCP and HTTP streaming based on their specific requirements for data delivery, scalability, security, and compatibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP streaming vs. REST
&lt;/h2&gt;

&lt;p&gt;HTTP streaming and &lt;a href="https://www.pubnub.com/guides/restful-apis/"&gt;REST&lt;/a&gt; are two different approaches to data delivery in the context of web applications.&lt;/p&gt;

&lt;p&gt;HTTP streaming refers to continuously sending data from a server to a client over an HTTP connection. It enables real-time delivery of multimedia content such as audio or video streams. HTTP streaming typically involves using protocols like HTTP Live Streaming (HLS) or Dynamic Adaptive Streaming over HTTP (DASH) to deliver the content in small chunks. This approach allows the client to play the media as it is being received without waiting for the entire file to download.&lt;/p&gt;

&lt;p&gt;On the other hand, REST (Representational State Transfer) is an architectural style for designing networked applications. It is based on principles and constraints that aim to make web services scalable, stateless, and interoperable. RESTful APIs (Application Programming Interfaces) use HTTP as the underlying communication protocol but do not involve real-time data streaming. Instead, REST APIs follow a request-response model, where a client sends a request to a server, and the server responds with a representation of the requested resource.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;HTTP Streaming vs. Long Polling&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When comparing HTTP streaming and &lt;a href="https://www.pubnub.com/guides/long-polling/"&gt;long polling&lt;/a&gt; for &lt;a href="https://www.pubnub.com/solutions/chat/"&gt;real-time chat&lt;/a&gt; and messaging applications, there are several factors to consider. Both methods have advantages and limitations, so it's important to understand their differences to make an informed decision.&lt;/p&gt;

&lt;p&gt;HTTP streaming is a technique where the server pushes data to the client over a single, long-lived connection. This allows for real-time message delivery, as updates can be sent to the client as soon as they are available. HTTP streaming is especially effective for applications that require low latency and high concurrency.&lt;/p&gt;

&lt;p&gt;On the other hand, long polling is a technique where the client sends a request to the server, and the server holds the request open until new data is available or a timeout occurs. This approach simulates real-time communication by continuously requesting the server to check for updates. Long polling can be useful for applications that don't require immediate updates and can tolerate slightly higher latency.&lt;/p&gt;

&lt;p&gt;One advantage of HTTP streaming over long polling is its ability to handle high concurrency and scale effortlessly. In contrast, long polling may struggle to efficiently handle large numbers of concurrent connections.&lt;/p&gt;

&lt;p&gt;Regarding performance, HTTP streaming has the advantage of low latency as updates are pushed to the client as soon as they are available. This real-time delivery of messages ensures that users receive the most up-to-date information without any delay. Conversely, long polling introduces a slight delay as the client needs to request the server to check for updates continuously.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the differences between HTTP streaming and other streaming protocols?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming, also known as HTTP-based adaptive streaming, is a streaming protocol that delivers multimedia content over regular HTTP (Hypertext Transfer Protocol) connections. It is different from other streaming protocols, such as &lt;a href="https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol"&gt;RTSP (Real-Time Streaming Protocol)&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol"&gt;RTMP (Real-Time Messaging Protocol)&lt;/a&gt;, in several ways:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transport Protocol:&lt;/strong&gt; HTTP streaming uses the HTTP protocol, widely supported by web servers, proxies, and firewalls. In contrast, RTSP and RTMP use transport protocols, which may require special configurations or dedicated infrastructure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Portability:&lt;/strong&gt; Since HTTP is a standard protocol, HTTP streaming can be easily accessed by various devices and platforms without needing specific client libraries or plugins. RTSP and RTMP, on the other hand, may require specialized clients or plugins to access the streamed content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; HTTP streaming leverages standard web infrastructure, allowing &lt;a href="https://www.cloudflare.com/learning/cdn/what-is-a-cdn/"&gt;content delivery networks (CDNs)&lt;/a&gt; to distribute the streaming content across multiple servers and locations efficiently. This enables better scalability and a wider reach for the streaming application. RTSP and RTMP, however, may require more complex setups to achieve similar scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adaptive Bitrate Streaming:&lt;/strong&gt; HTTP streaming supports &lt;a href="https://www.cloudflare.com/learning/video/what-is-adaptive-bitrate-streaming/#:~:text=Adaptive%20bitrate%20streaming%20adjusts%20video,%2C%20location%2C%20or%20Internet%20speed."&gt;adaptive bitrate streaming&lt;/a&gt;, which dynamically adjusts the video quality based on the viewer's available bandwidth and device capabilities. This ensures a smooth viewing experience, even under varying network conditions. RTSP and RTMP typically do not offer built-in adaptive bitrate streaming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Firewalls and Proxies:&lt;/strong&gt; HTTP streaming over HTTP can easily traverse firewalls and proxies since it uses the standard HTTP port (port 80 or 443 for HTTPS), usually open in most network configurations. In contrast, RTSP and RTMP may require specific port configurations or additional network setup to bypass firewalls and proxies.&lt;/p&gt;

&lt;p&gt;Overall, HTTP streaming offers advantages in terms of ease of implementation, portability, scalability, adaptive bitrate streaming, and compatibility with firewalls and proxies. These factors make it a preferred choice for developers building real-time chat and messaging applications that require scalability and security.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I set up an HTTP streaming server?
&lt;/h2&gt;

&lt;p&gt;Setting up an HTTP streaming server requires a few steps to ensure a seamless and efficient streaming experience. Here's a step-by-step guide to help you get started:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Choose a Streaming Protocol: There are several streaming protocols available, including HTTP Live Streaming (HLS), Dynamic Adaptive Streaming over HTTP (DASH), and Smooth Streaming. Select the protocol that best suits your requirements and target devices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install a Web Server: To stream content over HTTP, you'll need a web server. Popular options include &lt;a href="https://httpd.apache.org/"&gt;Apache HTTP Server&lt;/a&gt;, &lt;a href="https://www.pubnub.com/guides/nginx/"&gt;Nginx&lt;/a&gt;, and &lt;a href="https://www.iis.net/"&gt;Microsoft IIS&lt;/a&gt;. Choose one compatible with your operating system and install it on your server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure the Web Server: Once installed, you must configure your web server to handle streaming requests. This typically involves modifying the server's configuration file. Refer to the documentation specific to your chosen web server for detailed instructions on how to set up streaming.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prepare Your Content: Before streaming, you must prepare your content for delivery. This involves encoding your video files into the appropriate format, creating playlists, and segmenting the content into smaller chunks. Various encoding tools and software, like &lt;a href="https://ffmpeg.org/"&gt;FFmpeg&lt;/a&gt; or &lt;a href="https://helpx.adobe.com/media-encoder/using/overview-media-encoder-user-interface.html"&gt;Adobe Media Encoder&lt;/a&gt;, can assist you with this process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set Up Content Delivery: Consider using a content delivery network (CDN) to deliver your streaming content to users efficiently. A CDN caches your content on servers closer to your audience, reducing latency and improving streaming performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test and Monitor: After setting up your streaming server, it's essential to test and monitor its performance. Conduct thorough testing to ensure your streaming content is delivered smoothly and without glitches. Monitor key metrics such as buffering time, video quality, and network latency to identify potential issues and make necessary adjustments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scale as Needed: As your user base grows and the demand for your streaming service increases, you may need to scale your server infrastructure to handle the load. Consider using &lt;a href="https://www.pubnub.com/guides/load-balancing/"&gt;load balancers&lt;/a&gt;, clustering, or cloud-based solutions to ensure scalability and high availability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secure Your Streaming Server: To protect your streaming content and ensure the security of your server, implement appropriate security measures. This may include using encryption protocols such as HTTPS, enforcing access controls, and regularly updating your server software to patch any vulnerabilities.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What software is needed for HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;To enable HTTP streaming, several software components are required. The specific software needed depends on the streaming protocol being used. Here are the key components for the two most common HTTP streaming protocols:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP Live Streaming (HLS):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Media Encoder: Software like Apple's macOS-based media encoding tool "&lt;a href="https://www.apple.com/final-cut-pro/compressor/"&gt;Compressor&lt;/a&gt;" or FFmpeg can encode video and audio files into the required formats compatible with HLS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Media Segmenter: This software divides the encoded media into small, manageable chunks called segments. Apple's "&lt;a href="https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/UsingHTTPLiveStreaming/UsingHTTPLiveStreaming.html"&gt;mediafilesegmenter&lt;/a&gt;" or open-source tools like "Bento4" can perform this task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web Server: A web server, such as Apache or Nginx, is needed to serve the media files to clients over HTTP.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CDN (Content Delivery Network): A CDN may distribute the media files across multiple servers geographically, reducing latency and improving scalability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Dynamic Adaptive Streaming over HTTP (DASH):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Media Encoder: Similar to HLS, a media encoder must encode video and audio files into DASH-compatible formats. FFmpeg is a popular choice here as well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DASH Packager: This software packages the encoded media into the necessary DASH format (&lt;a href="https://ott.dolby.com/OnDelKits/DDP/Dolby_Digital_Plus_Online_Delivery_Kit_v1.5/Documentation/Content_Creation/SDM/help_files/topics/ddp_mpeg_dash_c_dash_mpd_ov.html#:~:text=Media%20Presentation%20Description-,A%20Media%20Presentation%20Description%20(MPD)%20is%20a%20hierarchical%20XML%20document,make%20up%20a%20media%20presentation."&gt;MPD - Media Presentation Description&lt;/a&gt;). Open-source tools like &lt;a href="https://github.com/gpac/gpac/wiki/MP4Box"&gt;MP4Box&lt;/a&gt; or commercial solutions like &lt;a href="https://bitmovin.com/"&gt;Bitmovin&lt;/a&gt; can perform this task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web Server: A web server is still needed to serve the media files to clients over HTTP, just like in HLS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CDN (Content Delivery Network) A CDN can distribute the media files across multiple servers for improved scalability and reduced latency.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to these components, you may need other software or tools depending on your specific requirements. These include media players for clients to play the streamed content, &lt;a href="https://www.fortinet.com/resources/cyberglossary/digital-rights-management-drm"&gt;DRM (Digital Rights Management&lt;/a&gt;) systems for content protection, and analytics tools for monitoring and analyzing the streaming performance.&lt;/p&gt;

&lt;p&gt;When considering the software for HTTP streaming, it's important to choose reliable and well-supported options compatible with your streaming infrastructure and meet your specific needs regarding features, scalability, and security.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are some APIs you can use for HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;Several APIs are available for HTTP streaming that developers can use to implement video content delivery in their applications. Here are some popular APIs for HTTP streaming:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3.org/TR/media-source-2/"&gt;&lt;strong&gt;Media Source Extensions&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(MSE):&lt;/strong&gt; MSE is a web API allowing JavaScript to generate playback media streams. It provides a way to dynamically switch between different media sources and adapt to network conditions. MSE is widely supported by modern browsers, making it a popular choice for HTTP streaming.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://videojs.com/"&gt;&lt;strong&gt;Video.js&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Video.js is an open-source JavaScript library that provides an HTML5 video player with support for HTTP streaming. It abstracts away the underlying video playback technology and provides a consistent API for developers. Video.js supports HLS and other streaming formats like MPEG-DASH and Smooth Streaming.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://cdn.dashjs.org/v2.4.0/jsdoc/index.html"&gt;&lt;strong&gt;Dash.js&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Dash.js is a reference client implementing the MPEG-DASH standard for HTTP streaming. It is an open-source JavaScript library that provides a feature-rich video player with support for adaptive bitrate streaming, DRM, captions, and more. Dash.js is widely used for MPEG-DASH streaming and offers extensive customization options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://exoplayer.dev/"&gt;&lt;strong&gt;ExoPlayer&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; ExoPlayer is an open-source media player library for Android that supports HTTP streaming. It provides developers with a flexible and extensible API to build custom media playback experiences. ExoPlayer supports HLS, MPEG-DASH, and Smooth Streaming, among other formats.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/avfoundation/avplayer/"&gt;&lt;strong&gt;AVPlayer&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; AVPlayer is a framework Apple provides for its iPhone’s iOS and macOS platforms. It supports HTTP streaming, including HLS, and offers advanced features such as adaptive bitrate streaming, subtitles, and offline playback. AVPlayer provides a high-level API for developers to integrate HTTP streaming into their applications easily.&lt;/p&gt;

&lt;p&gt;These are just a few examples of APIs available for HTTP streaming. Other APIs may better suit your needs depending on your specific requirements and the platforms you are targeting. It's important to research and evaluate different options to choose the one that aligns with your project goals and provides the necessary features and performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Programming languages can you use for HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;There are several programming languages that you can use for HTTP streaming, depending on your specific needs and preferences. Here are some popular options:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/guides/javascript/"&gt;JavaScript&lt;/a&gt; is widely used for web development and is the primary language for client-side scripting in web browsers. It is commonly used for implementing HTTP streaming on the front end, utilizing APIs such as Media Source Extensions (MSE) and libraries like Video.js and Dash.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/java"&gt;Java&lt;/a&gt; is a general-purpose programming language popular for building enterprise-level applications. It can be used for HTTP streaming on the server side, with frameworks like ExoPlayer and libraries that support streaming protocols such as HLS, MPEG-DASH, and Smooth Streaming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Swift:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/swift"&gt;Swift&lt;/a&gt; is a programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development. It can be used for HTTP streaming on Apple platforms, leveraging frameworks like AVPlayer that provide advanced features and support for streaming protocols like HLS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C#:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/c-sharp"&gt;C#&lt;/a&gt; is a programming language developed by Microsoft and primarily used to build Windows applications. It can be used for HTTP streaming on the server side, with frameworks and libraries that support streaming protocols such as HLS, MPEG-DASH, and Smooth Streaming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/python"&gt;Python&lt;/a&gt; is a versatile programming language known for its simplicity and readability. While it may not be the most common choice for HTTP streaming, libraries and frameworks such as Flask and Django can facilitate streaming implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/ruby"&gt;Ruby&lt;/a&gt; is a dynamic, object-oriented programming language known for its simplicity and productivity. Although it may not be as commonly used for HTTP streaming, libraries like EventMachine and Celluloid can be used for building streaming applications in Ruby.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/go"&gt;Go&lt;/a&gt; is a statically typed, compiled programming language designed for simplicity and scalability. It strongly focuses on concurrency and can be a good choice for building high-performance streaming applications. Some libraries like Gin and Revel can be used for HTTP streaming in Go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHP:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/php"&gt;PHP&lt;/a&gt; is a server-side scripting language widely used for web development. While it may not be the most popular choice for HTTP streaming, frameworks like Laravel and libraries like ReactPHP can be used to implement streaming functionality in PHP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rust:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/rust"&gt;Rust&lt;/a&gt; is a systems programming language known for its performance, reliability, and memory safety guarantees. While it may not be the most commonly used language for HTTP streaming, there are libraries like Tokio and Actix that can be leveraged for building streaming applications in Rust.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kotlin:&lt;/strong&gt; &lt;a href="https://www.pubnub.com/docs/sdks/kotlin"&gt;Kotlin&lt;/a&gt; is a statically typed programming language developed by JetBrains and officially supported for Android development. It can be used for HTTP streaming on Android platforms, using libraries like ExoPlayer that support streaming protocols such as HLS and MPEG-DASH.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Which HTTP streaming protocol is more flexible and offers interoperability?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Dynamic Adaptive Streaming over HTTP (DASH) is the HTTP streaming protocol that offers more flexibility and interoperability than other protocols. DASH is not tied to a specific platform or device, making it suitable for various applications and devices. It allows for efficient encoding and adaptive bitrate streaming, ensuring a smooth playback experience across different network conditions.&lt;/p&gt;

&lt;p&gt;One of the key advantages of DASH is its interoperability. It is supported by the MPEG industry consortium, which consists of major industry players, including Microsoft, Netflix, and Google. This widespread support ensures that DASH is compatible with various devices, platforms, and browsers. It provides a standardized approach to streaming video content, making it easier for developers to implement and maintain.&lt;/p&gt;

&lt;p&gt;In contrast, other streaming protocols like HTTP Live Streaming (HLS) are tied to specific platforms, such as Apple devices and browsers. While HLS is widely supported on Apple devices and browsers, its usage may be limited on other platforms. This can pose challenges for developers who want to ensure their video content reaches a broader audience.&lt;/p&gt;

&lt;p&gt;DASH also offers more flexibility in terms of encoding options. It supports various &lt;a href="https://www.pubnub.com/guides/webrtc/"&gt;video codecs&lt;/a&gt; and formats, allowing developers to choose the most efficient and suitable options for their specific use cases. This flexibility enables developers to optimize video quality and file sizes, enhancing the streaming experience for end-users.&lt;/p&gt;

&lt;p&gt;Furthermore, DASH's adaptive bitrate streaming capability allows for seamless playback across different network conditions. It adjusts the quality of the video in real time based on the viewer's internet connection, ensuring a smooth streaming experience even with varying network speeds. This adaptive bitrate streaming feature is crucial for real-time chat and messaging applications, allowing uninterrupted user communication.&lt;/p&gt;

&lt;p&gt;Another advantage of DASH is its ability to handle DRM and content protection. DASH supports various DRM systems, such as &lt;a href="https://www.microsoft.com/playready/"&gt;Microsoft PlayReady&lt;/a&gt; and &lt;a href="https://developers.google.com/widevine/drm/overview"&gt;Google Widevine&lt;/a&gt;, which are essential for protecting copyrighted content from unauthorized access. This ensures that developers can securely deliver their video content without compromising security.&lt;/p&gt;

&lt;p&gt;DASH is the more flexible and interoperable HTTP streaming protocol for developers building real-time chat and messaging applications. Its compatibility with a wide range of devices, platforms, and browsers, along with its support from major industry players, makes it a reliable choice. Additionally, its flexibility in encoding options and adaptive bitrate streaming capability further enhance the streaming experience for end-users.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Streaming vs. HTTP Live Streaming
&lt;/h2&gt;

&lt;p&gt;When comparing HTTP Streaming and HTTP Live Streaming (HLS), developers need several key factors to build real-time chat and messaging applications.&lt;/p&gt;

&lt;p&gt;First, HTTP Streaming is a more flexible and scalable protocol than HLS. While HLS is widely supported on Apple devices and browsers, its usage may be limited on other platforms. This can pose challenges for developers who want to ensure their video content reaches a broader audience. In contrast, HTTP Streaming is compatible with many devices, platforms, and browsers, making it a more reliable choice for developers.&lt;/p&gt;

&lt;p&gt;Secondly, HTTP Streaming offers more flexibility in terms of encoding options. It supports various video codecs and formats, allowing developers to choose the most efficient and suitable options for their specific use cases. This flexibility enables developers to optimize video quality and file sizes, enhancing the streaming experience for end-users.&lt;/p&gt;

&lt;p&gt;Furthermore, HTTP Streaming's adaptive bitrate streaming capability is crucial for real-time chat and messaging applications. It adjusts the quality of the video in real time based on the viewer's internet connection, ensuring a smooth streaming experience even with varying network speeds. This feature is essential for uninterrupted communication between users.&lt;/p&gt;

&lt;p&gt;Another advantage of HTTP Streaming, specifically DASH, is its handling of digital rights management (DRM) and content protection. DASH supports various DRM systems, such as Microsoft PlayReady and Google Widevine, which are essential for protecting copyrighted content from unauthorized access. This ensures that developers can securely deliver video content without compromising copyright protection.&lt;/p&gt;

&lt;p&gt;In comparison, HLS has limited support for DRM and content protection. While it does offer basic encryption options, it may not be sufficient for applications that require strong content protection measures. This can be a significant drawback for developers who need to ensure the security and integrity of their video content.&lt;/p&gt;

&lt;p&gt;Additionally, HTTP Streaming provides better support for real-time streaming and low-latency applications. It allows for faster delivery of video content, reducing buffering and latency issues. This is particularly important for real-time chat and messaging applications, where a delay in video delivery can impact the user experience and communication effectiveness.&lt;/p&gt;

&lt;p&gt;For several reasons, developers should consider using HTTP Streaming over HLS when building real-time chat and messaging applications. HTTP Streaming offers greater flexibility and scalability, compatibility with various devices and platforms, and support for various encoding options. Its adaptive bitrate streaming capability ensures a seamless streaming experience for users, regardless of their network connection. Moreover, HTTP Streaming's robust DRM and content protection features provide the necessary security measures for protecting copyrighted content.&lt;/p&gt;

&lt;p&gt;Developers can ensure a reliable, scalable, and secure platform for high-quality video content by choosing HTTP Streaming as the streaming protocol for real-time chat and messaging applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the use cases for HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming has many use cases, particularly in real-time chat and messaging applications. Here are some specific use cases for HTTP streaming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Video Streaming Services: HTTP streaming is commonly used in subscription streaming platforms like Netflix, YouTube, and Amazon Prime Video. It allows for the seamless delivery of high-quality video content to users across different devices and platforms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Live Streaming: HTTP streaming is widely used for &lt;a href="https://www.pubnub.com/solutions/live-audience-engagement/"&gt;live-streaming events&lt;/a&gt;, including sports tournaments, concerts, and conferences. It enables real-time video content broadcasting to a large audience, ensuring smooth playback and minimal buffering.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Video Conferencing: HTTP streaming is essential for video conferencing applications, where real-time communication and collaboration are crucial. It ensures smooth video playback and low-latency streaming, allowing participants clear, uninterrupted video calls.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gaming: HTTP streaming is increasingly used in cloud &lt;a href="https://www.pubnub.com/solutions/gaming/"&gt;gaming&lt;/a&gt; platforms, where games are streamed directly to users' devices over the internet. It allows gamers to play high-quality games without needing powerful hardware, as the processing is done on remote servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;E-Learning: HTTP streaming is utilized in &lt;a href="https://www.pubnub.com/solutions/multiuser-collaboration/"&gt;e-learning&lt;/a&gt; platforms that offer online courses and educational videos. It enables students to stream educational content seamlessly, regardless of location or device.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Social Media: HTTP streaming is employed by &lt;a href="https://www.pubnub.com/solutions/sports-media/"&gt;social media&lt;/a&gt; platforms like Twitch, LinkedIn, Facebook, Instagram, and Snapchat for live video streaming and sharing. It allows users to broadcast live videos to their followers and facilitates real-time interaction and engagement between users.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How secure is HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming protocols, such as HLS (HTTP Live Streaming) and DASH (Dynamic Adaptive Streaming over HTTP), provide security for online streaming content. However, it is essential to understand that while HTTP streaming protocols support secure transport mechanisms like HTTPS, overall security depends on various factors and considerations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Content Protection: HTTP streaming protocols do not inherently provide content protection mechanisms. Developers must implement additional measures such as DRM (Digital Rights Management) systems or encryption techniques to secure the streamed content. These measures ensure unauthorized users cannot access or replicate the streamed content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transport Security: HTTP streaming protocols can leverage secure transport mechanisms like HTTPS to encrypt the server and client communication. This encryption helps protect the integrity and confidentiality of the streamed content, preventing unauthorized interception or tampering.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authentication and Authorization: HTTP streaming protocols can integrate with authentication and authorization systems to ensure only authorized users can access the streaming content. This can involve user authentication, access control policies, and user management systems to regulate access rights.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server-Side Security: The security of the server infrastructure hosting the streaming content is crucial. Developers need to implement best practices for server security, including regular updates and patches, secure configurations, and monitoring for potential vulnerabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Network Security: While HTTP streaming protocols offer secure transport mechanisms, the overall security of the streaming system can be affected by potential network vulnerabilities. Developers should consider implementing additional security measures such as firewalls, intrusion detection systems, and network segmentation to protect against unauthorized access and attacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data Privacy: HTTP streaming protocols do not inherently provide data privacy features. Developers need to implement appropriate data privacy measures, such as encryption, to protect the personal and sensitive information transmitted during the streaming process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compliance: Depending on the content being streamed, developers may need to comply with industry-specific regulations and standards, such as &lt;a href="https://www.pubnub.com/blog/can-your-app-become-gdpr-compliant/"&gt;GDPR&lt;/a&gt; (General Data Protection Regulation) or &lt;a href="https://www.pubnub.com/guides/how-to-build-a-hipaa-compliant-app/"&gt;HIPAA&lt;/a&gt; (Health Insurance Portability and Accountability Act). Compliance with these regulations requires additional security measures and safeguards to protect user data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What industries can use HTTP streaming?
&lt;/h2&gt;

&lt;p&gt;HTTP streaming can be used in various industries that rely on real-time communication and content delivery. Some industries that can benefit from HTTP streaming include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Media and Entertainment: HTTP streaming is commonly used for live streaming platforms and online audio content. This includes streaming services, online gaming platforms, and live sports events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/solutions/ecommerce/"&gt;E-commerce&lt;/a&gt;: Online retailers can use HTTP streaming to showcase product videos and offer interactive shopping experiences. This enables customers to view product demonstrations and make informed purchasing decisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Education and e-learning: Educational institutions and e-learning platforms can leverage HTTP streaming to deliver live lectures, webinars, and on-demand educational content. This allows students and learners to access educational materials in real time or at their convenience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/solutions/digital-health/"&gt;Healthcare&lt;/a&gt;: HTTP streaming can be utilized in telemedicine applications, enabling providers to deliver remote consultations, video conferences, and medical training sessions. It can also be used for streaming patient education videos and medical content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Banking and Finance: Financial institutions can stream HTTP for real-time financial data feeds, stock market updates, and online trading platforms. This ensures clients have access to up-to-date information and improves overall user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gaming: The gaming industry heavily relies on HTTP streaming for online multiplayer games, game updates, and streaming platforms for game streaming services. This allows gamers to play and access game content in real time without extensive downloads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Social Media: Social media platforms often use HTTP streaming for their content creators to deliver live video broadcasts, live events, and real-time notifications to users. This enhances the user experience and allows for immediate interaction and engagement. Facebook Live is one example platform.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Communication and Collaboration: HTTP streaming can be used in communication and collaboration platforms, such as messaging apps, video conferencing tools, and file-sharing services. This enables real-time communication, file streaming, and collaborative workspaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/solutions/transport-delivery/"&gt;Transportation and Logistics&lt;/a&gt;: HTTP streaming can be applied to track and monitor vehicles, shipments, and inventory in transportation and logistics industries. This allows for efficient management and optimization of logistics processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/solutions/iot-device-control/"&gt;IoT (Internet of Things)&lt;/a&gt;: HTTP streaming can be utilized in IoT applications to transmit real-time sensor data, monitor and control signals, and device updates. This enables the integrating of IoT devices and systems for various industries, such as smart home automation, industrial automation, and environmental monitoring.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, HTTP streaming can benefit any industry requiring real-time communication, content delivery, and data transmission. By leveraging HTTP streaming, businesses can enhance their services, improve user experience, and stay competitive in today's digital landscape.&lt;/p&gt;

&lt;h2&gt;
  
  
  PubNub and HTTP Streaming
&lt;/h2&gt;

&lt;p&gt;PubNub is a leading platform that provides reliable and scalable real-time messaging and streaming services. It utilizes HTTP streaming as one of its core technologies to deliver seamless and efficient communication for developers building real-time chat and messaging applications.&lt;/p&gt;

&lt;p&gt;Developers can rely on the platform to handle millions of concurrent connections and deliver messages in real time, regardless of the geographical location of the users, thanks to the 15 Points-of-Presence (PoPs) worldwide...without any concurrency limits.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/trust/security/"&gt;Security&lt;/a&gt; is also a top priority, as the platform offers end-to-end encryption and authentication mechanisms to ensure that messages and data transmitted through HTTP streaming are secure and protected from unauthorized access.&lt;/p&gt;

&lt;p&gt;PubNub provides developers a scalable, secure, and feature-rich platform for building real-time chat and messaging applications. By leveraging our infrastructure, &lt;a href="https://www.pubnub.com/docs/sdks"&gt;SDKs&lt;/a&gt;, and extensive library of &lt;a href="https://www.pubnub.com/tutorials/"&gt;tutorials&lt;/a&gt;, developers can focus on creating innovative and engaging user experiences while PubNub takes care of the underlying complexities of real-time communication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://admin.pubnub.com/#/register"&gt;Sign up for a free trial&lt;/a&gt; and get up to 200 MAUs or 1M total transactions per month included.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://pubnub.com/guides/http-streaming/"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is ngrok?</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Fri, 24 Nov 2023 09:16:09 +0000</pubDate>
      <link>https://forem.com/pubnub/what-is-ngrok-3d3e</link>
      <guid>https://forem.com/pubnub/what-is-ngrok-3d3e</guid>
      <description>&lt;p&gt;ngrok is a cross-platform application that enables developers to expose a local development server to the Internet with minimal effort. The software makes your locally-hosted web server appear to be hosted on a subdomain of ngrok.com, meaning that no public IP or domain name on the local machine is needed. Similar functionality can be achieved with Reverse SSH Tunneling, but this requires more setup as well as hosting of your own remote server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ngrok pricing
&lt;/h2&gt;

&lt;p&gt;Ngrok has a free pricing tier for developers who are bringing new projects to life. The downside to the free tier is the lack of custom domain support.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does ngrok work?
&lt;/h2&gt;

&lt;p&gt;ngrok is able to bypass NAT Mapping and firewall restrictions by creating a long-lived &lt;a href="https://www.pubnub.com/guides/tcp-ip/"&gt;TCP&lt;/a&gt; tunnel from a randomly generated subdomain on ngrok.com (e.g. 3gf892ks.ngrok.com) to the local machine.&lt;/p&gt;

&lt;p&gt;After specifying the port that your web server listens on, the ngrok client program initiates a secure connection to the ngrok server and then anyone can make requests to your local server with the unique ngrok tunnel address. The &lt;a href="https://github.com/inconshreveable/ngrok/blob/master/docs/DEVELOPMENT.md"&gt;ngrok developer's guide&lt;/a&gt; contains more detailed information on how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is ngrok tunneling?
&lt;/h2&gt;

&lt;p&gt;Various tunnel servers are available around the world and locations include: US (Ohio), Europe (Frankfurt), Asia (Singapore), and Australia (Sydney). Alternatively, the ngrok server software can be self-hosted on a VPS or dedicated server.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use ngrok?
&lt;/h2&gt;

&lt;p&gt;By default, ngrok creates both &lt;a href="https://www.pubnub.com/guides/http/"&gt;HTTP&lt;/a&gt; and HTTPS endpoints, making it useful for testing integrations with third-party services or APIs that require valid &lt;a href="https://www.pubnub.com/guides/ssl-tls/"&gt;SSL/TLS&lt;/a&gt; domains. Other use cases include: quickly showcasing local demos to clients, testing mobile application backends, and running personal cloud services from your home PC.&lt;/p&gt;

&lt;p&gt;One praised feature of ngrok is the ability to track and replay HTTP requests via ngrok's web console . The replay functionality is highly useful when testing API calls or &lt;a href="https://www.pubnub.com/guides/webhooks/"&gt;webhooks&lt;/a&gt; as one can easily inspect all header content and request/response data in one place via the console UI.&lt;/p&gt;

&lt;p&gt;A premium version of ngrok, ngrok link, is available for developers to use in production and offers features such as API automation and credential management, making it suitable for remote IoT management in a professional setting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ngrok Security
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Ngrok is a highly secure platform for remote access and is safe to use&lt;/strong&gt;. Ngrok provides tunneling, as mentioned above, which enables users to access local-hosted servers from outside the machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do hackers use ngrok?
&lt;/h2&gt;

&lt;p&gt;Ngrok is regularly abused by hackers for cyber attacks. Hackers choose ngrok because of its capabilities and reputation to maneuver while bypassing security protocols. They will see this as a way to complete a phishing attack, as they are able to create fake login pages or deliver malicious URLs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/what-is-http-streaming/"&gt;Http Stream and Http Data&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/http-3/"&gt;Http 3 Info&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://pubnub.com/guides/what-is-ngrok/"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How Fast is Real-Time? Human Perception and Technology</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Wed, 22 Nov 2023 23:10:26 +0000</pubDate>
      <link>https://forem.com/pubnub/how-fast-is-real-time-human-perception-and-technology-1308</link>
      <guid>https://forem.com/pubnub/how-fast-is-real-time-human-perception-and-technology-1308</guid>
      <description>&lt;p&gt;In 1968 Robert Miller published his classic paper &lt;a href="https://www.google.com/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CCAQFjAA&amp;amp;url=http%3A%2F%2Ftheixdlibrary.com%2Fpdf%2FMiller1968.pdf&amp;amp;ei=zGTBVNCIPMWtogTWyYK4Dw&amp;amp;usg=AFQjCNEONWx__xLyqd6IsVTUGBxM0Z5EnQ&amp;amp;sig2=r9bBl-ESwyGUlsfWy36uNg&amp;amp;bvm=bv.84349003,d.cGU"&gt;&lt;em&gt;Response time in man-computer conversational transactions&lt;/em&gt;&lt;/a&gt; in which he described three different orders of magnitude of computer mainframe responsiveness:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A response time of 100ms is perceived as instantaneous.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Response times of 1 second or less are fast enough for users to feel they are interacting freely with the information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Response times greater than 10 seconds completely lose the user’s attention.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From this Miller concluded that a consistent 2-second response would be ideal. Years later this same value of &lt;a href="https://www.akamai.com/us/en/about/news/"&gt;2 seconds&lt;/a&gt; has been used as a performance target for web-based applications. Today’s real-time applications, however, require near-instantaneous responsiveness. Does even 100ms cut it?  The answer depends on the context.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Fast Can a Human Process Input?
&lt;/h2&gt;

&lt;p&gt;As humans beings, we have the curious inborn ability to observe and experience the persistent passage of time. The architecture of our human brains, however, limits our sensory perception in a way that prevents us from reacting to our perceptions within a certain short timeframe. This timeframe is commonly known as &lt;strong&gt;Reaction Time&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Human Reaction Time
&lt;/h2&gt;

&lt;p&gt;The average human reaction time is on the order of a quarter of a second (250 milliseconds). Don’t believe it? You can test your own reaction time with &lt;a href="https://www.humanbenchmark.com/tests/reactiontime"&gt;this little test.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you know, some humans have better reaction times than others. Fighter pilots, Formula One drivers, and championship video game players fall into the 100 – 120ms bucket on the left side of the curve.&lt;/p&gt;

&lt;p&gt;How much of that time is spent receiving data versus mentally processing and physically reacting?&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Time Latency: From Eye to Brain
&lt;/h2&gt;

&lt;p&gt;Reaction time is a complex subject and includes several different components of &lt;a href="https://www.frontiersin.org/articles/10.3389/fnhum.2020.00253/full"&gt;mental processing&lt;/a&gt; including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sensory perception&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Receipt of input into our consciousness&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Context applied to the input&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Decisions made based on processing output.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To really understand how fast real-time is to the human brain we’ll focus on the Sensory Perception phase. This is where our senses receive the incoming data from the outside world whether that be visual or auditory.&lt;/p&gt;

&lt;p&gt;For example, the time that an image of a tiger arriving on your retina takes to travel down your optic nerve into the visual cortex is incredibly fast. New studies show that &lt;a href="https://www.scienceabc.com/humans/how-fast-can-the-human-brain-process-images.html"&gt;&lt;strong&gt;humans can interpret visual cues seen for as little as 13 ms&lt;/strong&gt;&lt;/a&gt; (about 1 in 75 frames per second).&lt;/p&gt;

&lt;p&gt;As the brain receives the incoming data stream, an asynchronous process acknowledges the input and admits it into our consciousness. Now aware of the incoming data stream, another part of the brain applies context to the stream so that a decision can be made about how to react. All this happens very quickly. (&lt;a href="https://www.youtube.com/watch?v=TbUGwA_woCE"&gt;Cats are nearly twice as fast&lt;/a&gt;.)&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Unwanted Latency Impact Human Performance?
&lt;/h2&gt;

&lt;p&gt;While there is more involved in human reaction time than just mental processing, the important concepts here are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The fastest rate at which humans appear to be able to process incoming visual stimuli is about 13 ms&lt;/strong&gt;. Receiving a stream of data faster than this will only underscore the limits of our perception.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Increasing latency above 13 ms has an increasingly negative impact on human performance for a given task.&lt;/strong&gt; While imperceptible at first, added latency continues to degrade a human’s processing ability until approaching 75 to 100 ms. Here we become very conscious that input has become too slow and we must rely on adapting to conditions by anticipating input rather than simply reacting to input.&lt;/p&gt;

&lt;p&gt;In a duel, for example, a 100 ms lag matters. Especially if it is random and cannot be anticipated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implications for Real-Time Application Developers
&lt;/h2&gt;

&lt;p&gt;Real-time applications have varying tolerances to data stream latency. Typically those applications with very demanding targets include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/how-to/"&gt;Gaming&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/resources/data-sheet/bidding-and-auctions-with-pubnub/"&gt;Bidding &amp;amp; Auctions&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/customers/kakeai/"&gt;Collaboration&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.pubnub.com/industry/social-and-entertainment/"&gt;Live Entertainment&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is these types of applications in which real-time human perception and interaction are required. Given the resources required to build and maintain a real-time data stream network to support these types of applications, many developers make the strategic decision to outsource the messaging layer in order to focus more on the application itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Online Gaming
&lt;/h3&gt;

&lt;p&gt;While turn-based games, role-playing and strategy games typically do not rely on real-time movements or actions and they can tolerate latencies of up to 500ms or more, for &lt;a href="https://www.pubnub.com/industry/gaming/"&gt;Massive Multiplayer Online Gaming&lt;/a&gt; (MMOG), real-time is a requirement.&lt;/p&gt;

&lt;p&gt;As online gaming matures, players flock to games with more immersive and lifelike experiences. To satisfy this demand, developers now need to produce games with very realistic environments that have very strict data stream latency requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;300ms &amp;lt; game is unplayable&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;150ms  &amp;lt; gameplay degraded &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;100ms &amp;lt; player performance affected&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;50ms   &amp;gt; target performance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;13ms    &amp;gt; lower detectable limit&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A delay of even 100 ms reduces player performance in Twitch games by a measurable amount. It becomes noticeably difficult to track targets effectively and forces players into predicting movements.&lt;/p&gt;

&lt;p&gt;Overall game enjoyment continues to decrease as latency increases and players experience jerky playback, ghosting, and out-of-sync behavior that ultimately ruin the game for all players involved.&lt;/p&gt;

&lt;p&gt;Given these parameters, to be successful MMOG architecture must consider network performance as a fundamental requirement to ensure Quality of Experience for gamers. This architecture needs to be capable of delivering thousands of simultaneous data streams with latencies as low as 50 ms or better and to make it even more challenging, it must do so at scale for players in different geographic regions, on different access networks, using a range of devices.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“PubNub allows us to focus on our application, rather than the backbone network that supports it and the worries that accompany that. Knowing that we don’t have to setup a whole monitoring system to make sure our backbone network is running and sending messages is amazing; no crashing, no hardware reboots, and no worries,” said James Ross, co-founder and Operations Manager of NodeCraft Hosting.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bidding &amp;amp; Auctions
&lt;/h3&gt;

&lt;p&gt;With any auction, every split second counts. Success depends on making all bidders feel there’s seamless, reliable, real-time engagement for everyone, wherever in the world they may be. Having the right end-to-end platform for delivering real-time capabilities like real-time notifications, alerts, and updates is an imperative part of the user experience.&lt;/p&gt;

&lt;p&gt;A reliable and scalable platform solution is the key to online auction success because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It gives users the confidence they’re on top of every fresh bid or other development during an auction so they can make their next decisions about bidding (or not)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It maximizes participation among prospective bidders, contributing to higher top bids&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It strengthens the credibility of the auction and the organization conducting it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It keeps bidders continuously engaged by delivering the same lively, competitive feeling as a real-world, in-person auction&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-Time Collaboration
&lt;/h3&gt;

&lt;p&gt;Another interesting example of the use of real-time data is in the area of &lt;a href="https://www.pubnub.com/industry/elearning-and-collaboration/"&gt;e-learning and collaboration&lt;/a&gt;. In the &lt;a href="https://www.pubnub.com/industry/elearning-and-collaboration/"&gt;online classroom&lt;/a&gt;, it’s essential to have reliable real-time communication between devices. When dealing with a classroom full of students with short attention spans, devices need to be able to signal between each other as quickly as possible. Otherwise, you’re losing the attention of the entire class. However real-time data stream network design is an outside core competency for most.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“We didn’t know whether we should hire people to do that, whether we’d have to increase the size of the team, and eventually we just sat down and thought, ‘this isn’t what the core of our business is about. We shouldn’t really be spending loads of money and time trying to make real-time work, when we should be focusing on our own business challenges. Real-time was a requirement for our business, but not a business challenge that we should have to solve,”&lt;/em&gt; &lt;a href="https://www.pubnub.com/customers/classdojo/"&gt;&lt;em&gt;said Liam Don, Co-founder and CTO of ClassDojo&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Live Entertainment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The hallmark of a successful virtual event is if the audience is engaged with not only the performers but also each other. But just because virtual events have been part of our reality for a few years now, doesn’t mean that every event organizer (or platform) provides the necessary features to allow for engaging, interactive events.&lt;/p&gt;

&lt;p&gt;A few key features that developers should keep in mind when building apps for virtual events are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In-app chat with capabilities such as Presence, Reactions, and Typing Indicators&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Notifications and alerts to ensure viewers don’t miss a minute of the show—or messages from other participants&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unlimited conversation streams and concurrent users so there’s no need to worry about the event being &lt;em&gt;too&lt;/em&gt; successful and crashing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexible, real-time profanity filtering and translation to allow participants to communicate civilly, regardless of where they are located.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interactive polls, quizzes, and leaderboards synced in real time to keep your audience excited and truly engaged&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  PubNub and Real-Time
&lt;/h3&gt;

&lt;p&gt;Our &lt;a href="https://www.pubnub.com/learn/glossary/what-is-real-time-data/"&gt;real-time data API&lt;/a&gt; is flexible, so you can roll out these features quickly. Our globally available messaging infrastructure then lets you expand as your app, and needs, evolve. &lt;a href="https://www.pubnub.com/developers/"&gt;Getting started&lt;/a&gt; is easy. And, if you have any questions, we offer support 24/7 worldwide. With PubNub, you can start building real-time...today.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://www.pubnub.com/blog/how-fast-is-realtime-human-perception-and-technology/"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>7 Alternatives to REST APIs</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Wed, 22 Nov 2023 00:49:44 +0000</pubDate>
      <link>https://forem.com/pubnub/7-alternatives-to-rest-apis-p9m</link>
      <guid>https://forem.com/pubnub/7-alternatives-to-rest-apis-p9m</guid>
      <description>&lt;p&gt;Representational State Transfer (REST) is an architectural style and protocol for building web services. It is a popular approach for designing web application programming interfaces (APIs) because it emphasizes scalability, simplicity, and modifiability. &lt;/p&gt;

&lt;p&gt;Unlike the strict frameworks that govern API protocols like &lt;a href="https://www.pubnub.com/learn/glossary/what-is-soap-simple-object-access-protocol/"&gt;Simple Object Access Protocol&lt;/a&gt; (SOAP) or Extensible Markup Language remote procedure call (XML-RPC), REST protocols have historically been employed to simplify the process of API development, as they can be built using virtually any programming language and support a variety of data formats. &lt;/p&gt;

&lt;p&gt;However, the emergence of several REST alternatives is forming a new flashpoint for API development over the next decade. This trend includes other protocols, patterns, and technologies like &lt;a href="https://www.pubnub.com/guides/event-driven-architecture/"&gt;event-driven&lt;/a&gt; APIs, GraphQL, and gRPC. As these protocols achieve maturity and broader acceptance, it will become essential for API developers to understand how best to deploy REST alternatives across various platforms. &lt;/p&gt;

&lt;p&gt;Let's explore what makes these REST alternatives so popular and the most common use cases that support their use in the field.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why are REST alternatives gaining steam?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/restful-apis/"&gt;RESTful APIs&lt;/a&gt; are popular because they are flexible, easy to understand, and compatible with any programming language or platform that supports Hypertext Transfer Protocol (&lt;a href="https://www.pubnub.com/guides/http/"&gt;HTTP&lt;/a&gt;). They are also well-suited for building scalable and distributed systems, as they can leverage HTTP's caching and &lt;a href="https://www.pubnub.com/guides/load-balancing/"&gt;load-balancing&lt;/a&gt; capabilities. &lt;/p&gt;

&lt;p&gt;REST prioritizes easy modification through resources and Uniform Resource Identifiers (URIs) representing data. This means developers can change the API structure without breaking existing client applications.&lt;/p&gt;

&lt;p&gt;So why would developers use anything else? There are a few compelling reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Evolving complexity.&lt;/strong&gt; REST APIs are designed to overcome the complexity of earlier API protocols like SOAP, but they can become challenging to maintain as the number of endpoints and resources increases. This can make it difficult for developers to understand and modify the API over time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance.&lt;/strong&gt; For some use cases, REST APIs are scalable and can handle many requests. However, there may be better choices for real-time or low-latency applications, as they rely on multiple round-trip requests to retrieve data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Evolving data requirements.&lt;/strong&gt; REST APIs may require significant changes to support new use cases or data structures, leading to versioning and compatibility issues and increasing complexity and development time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Specific use case requirements.&lt;/strong&gt; There are specific use cases, such as real-time data streaming or low-power &lt;a href="https://www.pubnub.com/solutions/iot-device-control/"&gt;Internet of Things (IoT) devices&lt;/a&gt; (as mentioned above), where other protocols may be better suited than REST.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Developer preference.&lt;/strong&gt; Developers may prefer using alternative protocols because they are more familiar with them or offer specific features or benefits that REST does not provide.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Alternatives to REST APIs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are the seven REST alternatives you need to know:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;GraphQL&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;GraphQL is a runtime and query language for APIs that allows clients to request and receive only the data they require, making it more efficient than REST. With GraphQL, clients can specify the exact data they need and get it in a single request instead of making multiple requests to different endpoints as in REST. It's a great choice for data-driven applications with complex and evolving data requirements.&lt;/p&gt;

&lt;p&gt;It may not be the best choice for applications that require strict data validation, applications that need to support a wide variety of clients, or user-facing apps like social media. Still, it is an excellent alternative to REST in situations requiring flexible and efficient data retrieval and manipulation. This is particularly true in applications with complex data models or mobile applications with limited connectivity.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;gRPC&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;gRPC is an open-source framework developed by Google for building RPC APIs. It allows developers to define service interfaces and generate client and server code in multiple programming languages. gRPC uses protocol buffers and a language-agnostic data serialization format for efficient data transfer, making it ideal for high-performance applications.&lt;/p&gt;

&lt;p&gt;gRPC may not be the best choice for a large volume of data manipulation or for applications that need to support a wide variety of clients. However, gRPC is known for its high performance and low overhead, making it a good pick for applications that require fast and efficient communication between services.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;WebSockets&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/guides/what-are-websockets-and-when-should-you-use-them/"&gt;WebSocket protocol&lt;/a&gt; enables bi-directional, real-time communication between clients and servers. Unlike REST, which is request/response-based, WebSockets allow servers to push data to clients as soon as it becomes available, making it ideal for applications that require real-time updates, such as chat applications and online gaming.&lt;/p&gt;

&lt;p&gt;WebSockets may not be &lt;a href="https://www.pubnub.com/blog/websockets-alternatives/"&gt;the best choice&lt;/a&gt; for applications that require complex data manipulation or for applications where scalability is a concern. However, it shines where real-time communication and low latency are required, thanks to a full-duplex, persistent connection between client and server. REST uses a less efficient request/response model.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MQTT&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/mqtt/"&gt;MQTT&lt;/a&gt; is a lightweight, open-source messaging protocol designed for IoT devices. It's a &lt;a href="https://www.pubnub.com/guides/everything-you-need-to-know-about-pub-sub/"&gt;pub/sub protocol&lt;/a&gt; with a small packet size and low bandwidth, making it ideal for constrained networks and devices with limited processing power. MQTT can also handle intermittent network connectivity and supports Quality of Service (QoS) levels to ensure reliable message delivery.&lt;/p&gt;

&lt;p&gt;It's not the best choice for complex interactions or data manipulation applications. But for use cases with lower bandwidth and battery life preservation — MQTT allows devices to "sleep" between messages, extending the battery life of IoT devices — it offers excellent capability.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Event-Driven Architecture (EDA)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In EDA, events trigger and communicate changes between different components or services within a system. This allows for real-time and reactive data processing and can reduce the need for repeated polling of resources, which can be resource-intensive and time-consuming in REST-based systems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/docs/general/basics/add-serverless-business-logic"&gt;EDA&lt;/a&gt; is a good REST alternative for applications that require real-time data processing, scalability, and loose coupling between different components or services within a system. It's also well-suited for microservices architectures, allowing each microservice to operate independently and communicate with other services through events. This enables better scalability, flexibility, and resilience of the overall system.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;FALCOR&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Companies can also drive innovation in development. FALCOR is a &lt;a href="https://www.pubnub.com/guides/javascript/"&gt;JavaScript&lt;/a&gt; library developed by Netflix for building efficient and flexible APIs. It uses a "path-based" approach to data retrieval, representing data as a graph of interconnected paths rather than individual resources accessed through HTTP requests. It offers benefits like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;WebSocket support, which allows real-time data updates to be pushed to clients without requiring repeated polling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Declarative data fetching, where the client specifies the data it needs and the API responds with the requested data. This simplifies &lt;a href="https://www.pubnub.com/blog/chat-application-architecture-explained/"&gt;the client-side&lt;/a&gt; code and reduces the amount of data sent over the network.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The name "FALCOR" does not stand for anything. It was chosen by the developers at Netflix to represent the library's approach to data retrieval. It's inspired by the character of the same name from the 1980s movie "The NeverEnding Story," a dragon-like creature that can travel through different dimensions and worlds, much like FALCOR's ability to navigate complex data graphs.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Functions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;PubNub’s &lt;a href="https://www.pubnub.com/products/functions/"&gt;Functions&lt;/a&gt; are JavaScript event handlers that can be executed on in-transit PubNub messages or in the request/response style of a RESTful API over HTTPS. For those who are familiar with Node.js and Express, the On Request event handler development is very similar. Functions can deploy new or additional real-time functionality like chat moderation and language translation.&lt;/p&gt;

&lt;p&gt;Run your code within our network or leverage our existing integrations to transform, re-route, augment, filter and aggregate messages to detect and block spammers, measure averages, and more. All code is run at the edge for low latency and is robust enough that you don’t need to build your own infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Facilitating innovation one REST alternative at a time&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;REST alternatives are gaining popularity due to their ability to solve challenges REST APIs face. Each alternative has its own unique strengths and use cases. When choosing a REST alternative, it's essential to consider the specific needs and requirements of the application — whether its scale for &lt;a href="https://www.pubnub.com/solutions/live-events/"&gt;live events&lt;/a&gt; or real-time capabilities for &lt;a href="https://www.pubnub.com/solutions/web3/"&gt;Web3&lt;/a&gt; — as well as the strengths and limitations of each REST alternative. &lt;/p&gt;

&lt;p&gt;Ultimately, by exploring these alternatives, developers can build more efficient and effective APIs that meet the needs of modern applications.&lt;/p&gt;

&lt;p&gt;Make your app come to life. &lt;a href="https://www.pubnub.com/company/contact-sales/?&amp;amp;utm_source=clearvoice&amp;amp;utm_medium=blog&amp;amp;utm_campaign=contact_sales_cta"&gt;Contact us&lt;/a&gt; to discuss your real-time project or, better yet, &lt;a href="https://admin.pubnub.com/#/register"&gt;see it in action for yourself&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://pubnub.com/blog/7-alternatives-to-rest-apis/"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is Long Polling?</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Tue, 21 Nov 2023 10:51:31 +0000</pubDate>
      <link>https://forem.com/pubnub/what-is-long-polling-521h</link>
      <guid>https://forem.com/pubnub/what-is-long-polling-521h</guid>
      <description>&lt;h2&gt;
  
  
  What is long polling?
&lt;/h2&gt;

&lt;p&gt;Long polling is used in real-time web applications to achieve near-instantaneous communication between the client and the web server. It is particularly useful in chat and messaging applications where real-time updates are crucial.&lt;/p&gt;

&lt;p&gt;In traditional &lt;a href="https://www.pubnub.com/guides/http/"&gt;HTTP&lt;/a&gt; communication, the client sends a new request to the server and waits for a response. This is known as short polling. However, in real-time scenarios, short polling may not be efficient as it requires frequent requests to the server, resulting in unnecessary network overhead and increased latency.&lt;/p&gt;

&lt;p&gt;On the other hand, long polling improves efficiency by keeping the request open for an extended period until new data is available. The server holds the request open and waits until it has new information to send back to the client. Once the server has new data, it responds to the client, which can then process the data and initiate a new long polling request.&lt;/p&gt;

&lt;p&gt;By maintaining a long-lived connection between the client and the server, long polling reduces the number of requests, minimizes latency, and improves real-time communication. This makes it ideal in use cases that require an effective technique for building scalable and responsive chat and messaging applications as well as other apps that make use of real time data like games.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does long polling work?
&lt;/h2&gt;

&lt;p&gt;Long polling is a technique used in real-time communication to achieve near-instantaneous message delivery between clients and servers. It is particularly useful in building chat and messaging applications where low latency and real-time updates are crucial.&lt;/p&gt;

&lt;p&gt;Traditionally, web browsers use a pull-based approach to fetch data from servers. The client sends a request to the server, which responds with the requested data. This approach, known as short polling, can create delays in communication as the client has to send requests to check for updates repeatedly.&lt;/p&gt;

&lt;p&gt;On the other hand, long polling is a push-based approach that allows the server to send updates to the client as soon as they are available. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The client initiates a request to the server, typically through an HTTP request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of immediately responding, the server holds the request open, keeping the connection active.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If no new data is available, the server waits until it has something to send back.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the server has new data or a predefined timeout occurs, it responds to the client with the latest information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Upon receiving the response, the client immediately sends another request to the server to maintain the connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This cycle of sending requests and receiving responses continues, ensuring real-time updates.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Long polling effectively simulates a real-time connection between the client and the server by keeping the request-response cycle open for an extended period. It allows the server to push updates to the client as soon as they are available and eliminates the need for the client to check for updates repeatedly.&lt;/p&gt;

&lt;h2&gt;
  
  
  What technologies are used to implement long polling?
&lt;/h2&gt;

&lt;p&gt;Long polling is a technique to achieve real-time communication between a client and server. It is commonly used in chat and messaging applications where instant updates are crucial. Several technologies can be used to implement long polling, each with advantages and considerations. Let's explore some of the common technologies used in implementing long polling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/blog/http-long-polling/"&gt;&lt;strong&gt;HTTP long polling&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the most basic and widely used approach to implement long polling. It leverages the HTTP protocol to establish and maintain a long-lived connection between the client and server. The client sends a request to the server, and the server holds the request open until new data is available or a certain timeout is reached. Once new data is available, the server responds with the updated information, and the client immediately sends another request to continue the cycle. This approach is easy to implement and requires no special server-side technologies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/what-are-websockets-and-when-should-you-use-them/"&gt;&lt;strong&gt;WebSocket:&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WebSocket is a full-duplex communication protocol that enables real-time communication between the client and server over a single, long-lived connection. It provides a more efficient and low-latency alternative to long polling. WebSocket enables bidirectional data flow, allowing the client and server to send messages asynchronously. It eliminates the need for frequent HTTP requests and reduces network overhead. WebSocket is well-suited for applications requiring instant updates and real-time interaction.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/server-sent-events/"&gt;&lt;strong&gt;Server-Sent Events (SSE):&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SSE is a unidirectional communication technology that allows the server to push data to the client over a single, long-lived HTTP connection. With SSE, the server can send multiple updates to the client without requiring the client to make requests continuously. The server initiates the connection and sends data as a series of events. The client receives these events and can handle them as needed.&lt;/p&gt;

&lt;p&gt;When choosing a technology for implementing long polling in your application, there are several factors to consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Scalability: Ensure that the chosen technology can handle a large number of concurrent connections and can scale as the user base grows. WebSocket and SSE are generally more scalable than HTTP-based long polling, as they allow for more efficient use of server resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Security: Consider the security implications of the chosen technology. WebSocket and SSE can be secured using encryption protocols such as &lt;a href="https://www.pubnub.com/guides/ssl-tls/"&gt;SSL/TLS&lt;/a&gt;, ensuring data privacy and integrity. HTTP-based long polling can also be secured but may require additional authentication and access control measures.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Browser support: Check the browser compatibility of the chosen technology. WebSocket and SSE have better browser support than HTTP-based long polling, which may require additional techniques or fallback options for older browsers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementation complexity: Evaluate the chosen technology's ease of implementation and maintenance. HTTP-based long polling is relatively simple, while WebSocket and SSE may require more advanced knowledge and infrastructure. Consider the level of expertise available in your development team and the resources required for implementing and maintaining the chosen technology.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Long polling vs. WebSockets&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/blog/long-polling-vs-websockets/"&gt;Long polling and WebSockets&lt;/a&gt; are techniques to achieve a real-time connection between a client (such as a web browser) and a server. Although they serve a similar purpose, the two have significant differences.&lt;/p&gt;

&lt;p&gt;Long polling is a technique where the client makes a request to the web server, and the server keeps the connection open until it has new data to send back. The server may respond immediately if it has new data available or wait for a specified timeout period before sending an empty response. In either case, once the client receives the response, it immediately makes another request to the server to establish a new connection. This process repeats continuously, allowing the server to push updates to the client as soon as they become available.&lt;/p&gt;

&lt;p&gt;On the other hand, WebSockets provide a persistent, bidirectional communication channel between the client and the server. Unlike long polling, where a new connection is established for every request, a WebSocket connection is established once and kept open indefinitely. This allows real-time, low-latency communication in both directions. The server can push data to the client anytime, and the client can also send data to the server without waiting for a response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Similarities between long polling and Web Sockets:
&lt;/h3&gt;

&lt;p&gt;1. Real-time updates: Both long polling and WebSockets enable real-time communication between the server and client, allowing instant updates without continuous polling or refreshing.&lt;/p&gt;

&lt;p&gt;2. Reduced server load: Both techniques minimize unnecessary requests by only sending data when it is available, reducing server load and improving scalability.&lt;/p&gt;

&lt;p&gt;3. Wide language and framework support: Many &lt;a href="https://www.pubnub.com/docs/sdks"&gt;popular programming languages&lt;/a&gt; and frameworks support long polling and WebSockets, making them accessible to developers across different ecosystems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences between long polling and Web Sockets:
&lt;/h3&gt;

&lt;p&gt;1. &lt;a href="https://www.pubnub.com/guides/whats-so-important-about-low-latency/"&gt;Latency&lt;/a&gt;: Long polling introduces latency as there is a delay between the server sending a response and the client receiving it. WebSockets provide bidirectional, low-latency communication, allowing for faster real-time capabilities.&lt;/p&gt;

&lt;p&gt;2. Resource consumption: Long polling requires the server to maintain open connections with each client, potentially causing resource consumption and limiting the number of concurrent connections. WebSockets, however, use a persistent connection, reducing the overall resource consumption.&lt;/p&gt;

&lt;p&gt;3. Scalability: Long polling's need to keep connections open for an extended period can pose challenges for horizontally scaling the server. With their persistent connection, WebSockets allow for better scalability as they do not require many open connections.&lt;/p&gt;

&lt;p&gt;Both long polling and WebSockets offer real-time updates and reduce server load, but they differ in latency, resource consumption, and scalability. Web sockets provide faster, bidirectional communication with reduced resource consumption, making them suitable for applications that require low latency and high scalability. On the other hand, long polling can be a good alternative when low latency is not critical, and the number of concurrent connections is relatively small. Developers should consider these factors when choosing between the two techniques for their &lt;a href="https://www.pubnub.com/guides/real-time-messaging-benefits-basics-and-first-steps/"&gt;real-time chat and messaging applications&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Long polling vs. Server-Sent Events (SSE)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/blog/websockets-alternatives/"&gt;SSE&lt;/a&gt; is similar to long polling in terms of its simplicity and ease of implementation, but it provides a more efficient and standardized way of achieving server-to-client communication. Let’s look at some additional similarities and differences between the two technologies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Similarities between long polling and SSE:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Real-time updates: Both long polling and SSE enable real-time communication between the server and client, allowing instant updates without continuous polling or refreshing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduced server load: Both techniques minimize unnecessary requests by only sending data when it is available, reducing server load and improving scalability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wide language and framework support: Many popular programming languages and frameworks support long polling and SSE, making them accessible to developers across different ecosystems.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Differences between long polling and SSE:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Latency: Long polling introduces latency as there is a delay between the server sending a response and the client receiving it. On the other hand, SSE provides a continuous stream of data from the server to the client, reducing latency and improving real-time capabilities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resource consumption: Long polling requires the server to maintain open connections with each client, potentially causing resource consumption and limiting the number of concurrent connections. SSE, however, uses a single long-lived connection, reducing the overall resource consumption.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability: Long polling's need to keep connections open for an extended period can pose challenges for horizontally scaling the server. With its single connection per client, SSE allows for better scalability as it does not require many open connections.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How can you optimize long polling?
&lt;/h2&gt;

&lt;p&gt;Long polling is used in real-time chat and messaging applications to provide near-instantaneous client updates. However, it can be resource-intensive and cause scalability issues if not optimized properly. Here are several techniques that can be used to optimize long polling for better performance and scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Batched responses:&lt;/strong&gt; Instead of sending a response for each request, &lt;a href="https://developers.google.com/classroom/best-practices/batch#:~:text=separate%20HTTP%20request.-,Response%20to%20a%20batch%20request,same%20order%20as%20the%20requests."&gt;batch multiple updates together&lt;/a&gt; and send them in a single response. This reduces the number of HTTP requests and helps to minimize the overhead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compression:&lt;/strong&gt; Compressing the data before sending it over the network can significantly reduce the payload size, resulting in faster transmission and lower bandwidth consumption. Techniques like &lt;a href="https://en.wikipedia.org/wiki/Gzip"&gt;Gzip compression&lt;/a&gt; can be used to achieve this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching:&lt;/strong&gt; Implementing a &lt;a href="https://aws.amazon.com/caching/"&gt;caching layer&lt;/a&gt; can help reduce the load on the database or other data sources. By caching the frequently requested data, subsequent requests can be served from the cache itself, reducing the response time and improving scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connection pooling:&lt;/strong&gt; Maintaining a pool of reusable connections instead of creating a new connection for every request can improve the efficiency of the long polling mechanism. This eliminates the overhead of establishing a new connection for each request, resulting in better performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Throttling and rate limiting:&lt;/strong&gt; Implementing throttling and rate limiting mechanisms can prevent excessive requests from overwhelming the server. This ensures fair resource allocation and prevents abuse, improving performance and scalability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pubnub.com/guides/load-balancing/"&gt;&lt;strong&gt;Load balancing&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;:&lt;/strong&gt; Distributing the incoming requests across multiple servers using load balancing techniques can help distribute the load and prevent any single server from becoming overwhelmed. This improves the overall performance and scalability of the long polling system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Monitoring and optimization:&lt;/strong&gt; Regularly monitoring the performance of the long polling system and identifying any bottlenecks or areas of improvement can help optimize the system for better performance and scalability. Techniques like profiling, load testing, and performance tuning can be used to identify and address any performance issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous (Async) processing:&lt;/strong&gt; Offloading time-consuming tasks to &lt;a href="https://www.pubnub.com/blog/asynchronous-tcp-publishing-on-a-single-tcp-payload/"&gt;asynchronous processes&lt;/a&gt; or background workers can help free up resources and improve the responsiveness of the long polling system. You can get this via message queues, worker processes, or distributed computing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connection timeouts:&lt;/strong&gt; Implementing appropriate connection timeouts can help prevent idle connections from consuming unnecessary resources. By closing idle connections after a certain period of inactivity, the system can free up resources for other clients and improve scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalable infrastructure:&lt;/strong&gt; Ensuring the underlying infrastructure is scalable and can handle the expected load is crucial for optimizing long polling. This may involve using technologies like cloud computing, auto-scaling, or &lt;a href="https://www.pubnub.com/guides/containerization/"&gt;containerization&lt;/a&gt; to dynamically allocate resources based on demand.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Programming Languages are Compatible with Long Polling?
&lt;/h2&gt;

&lt;p&gt;Several programming languages are compatible with implementing long polling in real-time chat and messaging applications. Here are a few examples:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;JavaScript: Long polling is commonly combined with &lt;a href="https://www.pubnub.com/blog/the-comprehensive-pubnub-publish-subscribe-javascript-api-tutorial/"&gt;JavaScript&lt;/a&gt;, allowing for seamless client-side implementation. &lt;a href="https://www.pubnub.com/guides/javascript/"&gt;JavaScript&lt;/a&gt; frameworks like &lt;a href="https://www.pubnub.com/blog/building-a-chat-app-with-react-native-and-pubnub-part-one-messaging/"&gt;React&lt;/a&gt;, Angular, and &lt;a href="https://www.pubnub.com/blog/building-a-laravel-chat-app-with-php-vue-js-and-pubnub/"&gt;Vue.js&lt;/a&gt; provide libraries and tools that simplify implementing long polling in your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PHP: &lt;a href="https://www.pubnub.com/docs/sdks/php"&gt;PHP&lt;/a&gt; is a popular server-side language often used in web development. It provides features and libraries that enable developers to implement long polling efficiently. The PHP framework Laravel, for example, offers support for long polling through its event broadcasting system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python: &lt;a href="https://www.pubnub.com/blog/socket-programming-in-python-client-server-p2p/"&gt;Python&lt;/a&gt; is another versatile language that can be used for implementing long polling. Python frameworks like Django and Flask provide the tools and libraries for building real-time applications using long polling techniques.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ruby: &lt;a href="https://www.pubnub.com/docs/sdks/ruby"&gt;Ruby&lt;/a&gt; is a dynamic, object-oriented programming language well-suited for web development. A popular web framework, &lt;a href="https://rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt;, supports long polling through various libraries and extensions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java: &lt;a href="https://www.java.com/en/"&gt;Java&lt;/a&gt; is a widely used language in enterprise development and provides support for long polling. Java frameworks like Spring and Java EE offer libraries and tools for implementing long polling in real-time applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.NET/C#: The &lt;a href="https://dotnet.microsoft.com/en-us/learn/dotnet/what-is-dotnet-framework"&gt;.NET framework&lt;/a&gt;, with its &lt;a href="https://www.pubnub.com/docs/sdks/c-sharp"&gt;programming language C#&lt;/a&gt;, is commonly used for building web applications. It provides libraries and frameworks like ASP.NET &lt;a href="https://www.pubnub.com/guides/what-is-signalr/"&gt;SignalR&lt;/a&gt; that simplify the implementation of long polling techniques.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just a few examples of programming languages that support long polling. Many other languages and frameworks can also implement long polling in real-time chat and messaging applications.&lt;/p&gt;

&lt;p&gt;When choosing a programming language for implementing long polling, there are a few factors to consider. First, consider the specific requirements of your application and choose a language that best fits those requirements. Consider scalability, performance, and ease of implementation on the backend.&lt;/p&gt;

&lt;p&gt;Additionally, consider the community and ecosystem surrounding the programming language. A strong and active community can provide support, tutorials, documentation, and resources to make implementing long polling in your application easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is long polling used in real-time applications?
&lt;/h2&gt;

&lt;p&gt;One of the primary advantages of long polling is its efficiency in delivering real-time updates. Minimizing the number of requests sent by clients significantly reduces network latency and improves overall performance. Additionally, it allows servers to push updates to clients immediately, ensuring that messages and notifications are delivered promptly.&lt;/p&gt;

&lt;p&gt;Furthermore, long polling facilitates scalability in real-time applications. Reducing the number of open connections enables servers to handle more concurrent clients. This is particularly crucial in chat and messaging applications, where the number of users constantly fluctuates.&lt;/p&gt;

&lt;p&gt;Long polling helps save on system resources. With traditional polling, each request requires the server to process and respond, even without updates. This constant processing can strain server resources and negatively impact performance. In contrast, long polling only triggers server processing when new data is available or a timeout occurs. This minimizes the strain on system resources and allows for better scalability and reliability.&lt;/p&gt;

&lt;p&gt;However, implementing long polling can also introduce some challenges. One challenge is managing server resources effectively. Since long polling involves keeping connections open for extended periods, handling multiple concurrent connections requires significant server resources. This can be addressed using technologies like cloud computing, auto-scaling, or &lt;a href="https://www.pubnub.com/blog/buzzword-microservices-serverless-functions-programmable-infrastructur/"&gt;containerization&lt;/a&gt; to dynamically allocate resources based on demand. By automatically scaling resources up or down based on the number of active connections, developers can ensure that the server can handle the expected load effectively.&lt;/p&gt;

&lt;p&gt;Another challenge is handling timeouts and connection failures. In long polling, the server holds the request open until new data is available or a timeout occurs. If a timeout occurs, the server must handle it gracefully and close the connection to free up resources. Additionally, if a connection fails, the server should be able to detect it and handle reconnection attempts appropriately. By implementing robust error handling and connection management mechanisms, developers can ensure the reliability of long polling in real-time applications.&lt;/p&gt;

&lt;p&gt;Security is another important consideration when implementing long polling in real-time applications. Since long polling involves maintaining persistent connections between clients and servers, it is crucial to secure these connections to protect sensitive data. Implementing secure socket layers (SSL) or transport layer security (TLS) protocols can help encrypt data transmitted over long polling connections and prevent eavesdropping or unauthorized access.&lt;/p&gt;

&lt;p&gt;With over 15 points of presence worldwide supporting 800 million monthly active users and 99.999% reliability, you’ll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes. PubNub is perfect for any application that requires real-time data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://admin.pubnub.com/#/register"&gt;Sign up for a free trial&lt;/a&gt; and get up to 200 MAUs or 1M total transactions per month included.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can PubNub help you?
&lt;/h1&gt;

&lt;p&gt;This article was originally published on &lt;a href="https://pubnub.com/guides/long-polling/?d=35"&gt;PubNub.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.&lt;/p&gt;

&lt;p&gt;The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience PubNub
&lt;/h2&gt;

&lt;p&gt;Check out &lt;a href="https://www.pubnub.com/tour/introduction/"&gt;Live Tour&lt;/a&gt; to understand the essential concepts behind every PubNub-powered app in less than 5 minutes&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Setup
&lt;/h2&gt;

&lt;p&gt;Sign up for a &lt;a href="https://admin.pubnub.com/signup/"&gt;PubNub account&lt;/a&gt; for immediate access to PubNub keys for free&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Started
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.pubnub.com/docs"&gt;PubNub docs&lt;/a&gt; will get you up and running, regardless of your use case or &lt;a href="https://www.pubnub.com/docs"&gt;SDK&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Announcing the New PubNub Chat SDK</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Fri, 13 Oct 2023 10:20:51 +0000</pubDate>
      <link>https://forem.com/pubnub/announcing-the-new-pubnub-chat-sdk-18d3</link>
      <guid>https://forem.com/pubnub/announcing-the-new-pubnub-chat-sdk-18d3</guid>
      <description>&lt;p&gt;We are excited to announce the new &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/overview"&gt;PubNub Chat SDK&lt;/a&gt;, the easiest way for developers to create new chat applications or add in-app chat to existing applications.&lt;/p&gt;

&lt;p&gt;We frequently see customers implementing the same chat features over and over again with our traditional SDKs; for example, most chat apps will handle group messaging, show a typing indicator, and have a way of organizing users.  Rather than expect everyone to implement these high-level features using our traditional APIs, we thought: “Why not expose a dedicated API specifically for chat features?”  The Chat SDK was created to address that need.  Best of all, since the Chat SDK is still built on the PubNub platform, you get all the great PubNub features we offer, such as compliance, scalability, and security (through the Access Manager).&lt;/p&gt;

&lt;p&gt;Any developer targeting TypeScript can use the Chat SDK in their project.  We are framework-agnostic, so the Chat SDK is a great choice regardless of whether you are working with Angular, React, Vue, Ionic, or any other framework that supports TypeScript, both on web and mobile platforms.  Additionally, the Chat SDK is &lt;strong&gt;UI agnostic&lt;/strong&gt;, so it gives you the freedom to create unique chat experiences with either your own UI or any pre-built Chat UI available on the market. Why TypeScript?  This was in response to customer feedback and where we saw the most demand for what is our first use-case-specific API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features of the PubNub Chat SDK
&lt;/h2&gt;

&lt;p&gt;To add &lt;a href="https://www.pubnub.com/solutions/chat/"&gt;chat functionality&lt;/a&gt; to your application, the PubNub Chat SDK has been built to support the following features out of the box:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conversation (Channel) Management:&lt;/strong&gt; Create direct 1:1 conversations or larger group chats, inviting others to join or leave as needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Manage Users:&lt;/strong&gt; Detect users’ online status, mention them in conversations, and moderate their content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Basic Messaging:&lt;/strong&gt; Send and receive messages, emojis, links, and files to any number of recipients.   &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Advanced Messaging:&lt;/strong&gt; Forward, Quote, or Pin messages.  The SDK also supports read receipts, unread message counts, and a typing indicator to see if other channel participants are typing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mobile app support:&lt;/strong&gt; Easy integration with Android (FCM) and iOS (APNS) push messaging, giving you a consistent approach to offline messages without worrying about platform-specific implementation. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Chat SDK natively supports concepts such as ‘Users,’ ‘Messages,’ and ‘Channels,’ which are used as follows:&lt;/p&gt;

&lt;h2&gt;
  
  
  Users
&lt;/h2&gt;

&lt;p&gt;Create a &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/create"&gt;User&lt;/a&gt; to represent anybody who will participate in a chat conversation.  Users have all the properties you would expect of a chat system, like nickname &amp;amp; avatar, and you can assign custom properties as needed, such as an external ID to match the user with your external system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// reference the "chat" object and invoke the "createUser()" method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;support_agent_15&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Smith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;profileUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://randomuser.me/api/portraits/men/1.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;VP Marketing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;linkedInUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://www.linkedin.com/mkelly_vp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once created, APIs exist to &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/details"&gt;retrieve&lt;/a&gt;, &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/updates#update-user-details"&gt;update&lt;/a&gt;, &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/delete"&gt;delete&lt;/a&gt;, or &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/updates#get-user-updates"&gt;listen for any changes&lt;/a&gt; to a user’s properties. &lt;/p&gt;

&lt;h2&gt;
  
  
  Channels
&lt;/h2&gt;

&lt;p&gt;Having created representations for the system’s users, they need to be added to conversations.  The Chat SDK uses the concept of &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/create"&gt;channels&lt;/a&gt; to represent a conversation between users, and a conversation can either be &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/create#create-direct-channel"&gt;direct&lt;/a&gt;, between just two individuals, a &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/create#create-group-channel"&gt;group conversation&lt;/a&gt; amongst multiple users, or a &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/create#create-public-channel"&gt;public group&lt;/a&gt; without any limits on membership.&lt;/p&gt;

&lt;p&gt;Assuming two users have already been created, you can create a group conversation as follows.  Note that optional metadata can be added to the channel, such as name and purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// reference both agents you want to talk to&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;agent-007&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;agent-008&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// add the channel ID, name, and topic&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hostMembership&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inviteeMembership&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createGroupConversation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;users&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;user2&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;channelId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;group-chat-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="na"&gt;channelData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Weekly syncs on customer XYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;membershipData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;purpose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;premium-support&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The users passed in at channel creation only represents the initial state, you can &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/channels/join"&gt;join&lt;/a&gt; or &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/leave"&gt;leave&lt;/a&gt; an existing conversation at any time or other users can &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/channels/invite#invite-one-user"&gt;invite&lt;/a&gt; you to an existing channel.  Other APIs also exist to manage both the &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/updates"&gt;channel&lt;/a&gt; and &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/channels/membership"&gt;channel membership&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vmMEeVt9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0qkfgnkv4y4fpyvitab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vmMEeVt9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0qkfgnkv4y4fpyvitab.png" alt="Image description" width="780" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Messages
&lt;/h2&gt;

&lt;p&gt;With the user now a member of one or more channels, messages can be sent and received over that channel. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: You don’t strictly need to be a member of a channel to send and receive messages, but membership is required for more advanced features such as presence and mentions, so it is recommended.  See our &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/send-receive#receive"&gt;documentation&lt;/a&gt; for more details.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To receive messages, first &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/channels/watch"&gt;connect&lt;/a&gt; to the channel and specify a callback to be invoked when new messages arrive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// reference the "channel" object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;support&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// invoke the "connect()" method&lt;/span&gt;
&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Message received on channel: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sending messages is very flexible, but in its simplest form, the sendText method is used to send a basic text message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// reference the channel where you want to send a text message&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;support&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// invoke the "sendText()" method on the "channel" object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi, Everyone!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;storeInHistory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;messageImportance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;high&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the message is (optionally) persisted with &lt;code&gt;storeInHistory&lt;/code&gt; and additional metadata can be associated with the message.&lt;/p&gt;

&lt;p&gt;You are not limited to sending textual messages, you can send &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/files"&gt;files&lt;/a&gt; and &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/links"&gt;links&lt;/a&gt;, or react with &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/reactions"&gt;emojis&lt;/a&gt;.  Messages can be &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/forward"&gt;forwarded&lt;/a&gt;, &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/quotes"&gt;quoted&lt;/a&gt;, &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/pinned"&gt;pinned&lt;/a&gt;, and you can even spawn &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/threads"&gt;threads&lt;/a&gt; or receive &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/read-receipts"&gt;read receipts&lt;/a&gt; in reply.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hMmml0wL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsncooqbdi530ekt59oe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hMmml0wL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsncooqbdi530ekt59oe.png" alt="Image description" width="780" height="908"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Combining Users, Channels, &amp;amp; Messages
&lt;/h2&gt;

&lt;p&gt;Having configured your Users and Channels, as well as each user’s channel memberships, you can take advantage of powerful capabilities offered by the Chat SDK:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User Presence and Online Status:&lt;/strong&gt; Increase interactivity by tracking which users are active or away with the ‘&lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/presence"&gt;online status&lt;/a&gt;’ capability. See who is available on each channel or globally using the ‘channel &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/users/presence#global-presence"&gt;presence&lt;/a&gt;’ feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ng0lPG74--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1updbhpv4k52u1xmmeu3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ng0lPG74--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1updbhpv4k52u1xmmeu3.png" alt="Image description" width="341" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@Mention Users in Messages:&lt;/strong&gt; When composing messages, users can see a &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/users/mentions"&gt;list of suggested users&lt;/a&gt; when they start typing ‘@,’ sourced either globally or from only members of that channel.  Since the SDK stores the list of mentioned users as a separate data structure alongside the message, you can use it to send notifications according to your business logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kYwOCIqB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yayg6g6bufghhggzukjy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kYwOCIqB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yayg6g6bufghhggzukjy.png" alt="Image description" width="768" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;History:&lt;/strong&gt; All messages and files can be optionally stored in persistent storage and assigned a custom time to live (ttl).  Message storage is &lt;a href="https://www.pubnub.com/blog/were-soc-2-type-ii-compliant/"&gt;certified compliant&lt;/a&gt;, meaning you can build applications that require GDPR, HIPAA, or SOC2 with the Chat SDK.  This list is incomplete, so please &lt;a href="https://www.pubnub.com/company/contact-sales/"&gt;contact our expert team&lt;/a&gt; if you have specific requirements. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WI385wBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6ikd1a1gytptwwaa244.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WI385wBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6ikd1a1gytptwwaa244.png" alt="Image description" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit and Delete Messages:&lt;/strong&gt; After messages are sent, they can be optionally &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/updates"&gt;edited&lt;/a&gt; or &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/features/messages/delete"&gt;deleted&lt;/a&gt; as needed.  As the developer, you have complete control - you can choose to retain deleted messages in your history or delete them entirely.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NffJlIG5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/peglv584f7qfviwdof5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NffJlIG5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/peglv584f7qfviwdof5x.png" alt="Image description" width="672" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moderation:&lt;/strong&gt; The Chat SDK allows you to &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/moderation"&gt;report or flag&lt;/a&gt; offensive messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typing Indicator:&lt;/strong&gt; Dedicated APIs for start and stop typing allow you to track &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/typing-indicator"&gt;who is typing&lt;/a&gt; on any individual channel.  The Chat SDK will handle all the timeout logic for you, so you don’t have to worry about keeping track of arrays of actively typing users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yOFjswF1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n02oh4azoyf6kk5gr8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yOFjswF1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n02oh4azoyf6kk5gr8p.png" alt="Image description" width="741" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mobile Push Notifications:&lt;/strong&gt; PubNub has built-in support for &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/push-notifications"&gt;mobile push notifications&lt;/a&gt;, so regardless of whether your users are running Android or iOS, you can notify users with the same platform-agnostic Chat API you use to send messages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vm9sJSTp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrzgwi1of2g4gld27mrd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vm9sJSTp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrzgwi1of2g4gld27mrd.png" alt="Image description" width="780" height="809"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation Details
&lt;/h2&gt;

&lt;p&gt;These are the high-level building blocks of the chat SDK.  Please check out our &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/configuration"&gt;docs&lt;/a&gt; or the &lt;a href="https://github.com/pubnub/js-chat/tree/master/samples/react-native-group-chat"&gt;full demo&lt;/a&gt; to learn more about the implementation details.  Additional resources can also be found at the end of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting an SDK: The Chat SDK or Another PubNub SDK?
&lt;/h2&gt;

&lt;p&gt;Until the release of the &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/learn/why-chat-sdk"&gt;PubNub Chat SDK&lt;/a&gt;, developers would use one of our traditional SDKs, for example, the &lt;a href="https://www.pubnub.com/docs/sdks/javascript"&gt;JavaScript SDK&lt;/a&gt;, and build chat functionality based on the general JavaScript API.  This is certainly possible to do, and thousands of developers have already done so, but the process was more complicated than it needed to be for developers specifically developing chat apps.&lt;/p&gt;

&lt;p&gt;Let’s take, for example, how to implement an unread message counter in both the JavaScript SDK and the Chat SDK:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M5nLC1JJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0jh34kmkrn3gzyezmgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M5nLC1JJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0jh34kmkrn3gzyezmgy.png" alt="Image description" width="780" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  An unread message counter using the JavaScript SDK:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;For the current user, viewing some specific channel, set the timetoken to indicate the latest message the user viewed.  &lt;/p&gt;

&lt;p&gt;The recommended way to do this is to use PubNub AppContext to store metadata associated with the user’s membership of the channel, setting &lt;code&gt;lastReadTimetoken&lt;/code&gt; as required.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To determine the number of unread messages on any specific channel, call the &lt;a href="https://www.pubnub.com/docs/sdks/javascript/api-reference/storage-and-playback#message-counts"&gt;messageCounts API&lt;/a&gt; and pass in both: &lt;/p&gt;

&lt;p&gt;The channel name and the last viewed timetoken, as stored in PubNub AppContext and set in the previous step.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is how our &lt;a href="https://github.com/PubNubDevelopers/PubNub-Showcase/tree/main"&gt;showcase app&lt;/a&gt; implements the unread message count when the app first loads; specifically, please see &lt;a href="https://github.com/PubNubDevelopers/PubNub-Showcase/blob/main/web/chat/message.js"&gt;message.js&lt;/a&gt; and &lt;a href="https://github.com/PubNubDevelopers/PubNub-Showcase/blob/main/web/chat/chat.js"&gt;chat.js&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  An unread message counter using the Chat SDK:
&lt;/h3&gt;

&lt;p&gt;As detailed in the Chat SDK documentation for &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/unread"&gt;unread messages&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;For the current user’s channel membership, specify the last read message using the &lt;code&gt;setLastReadMessage()&lt;/code&gt; API, which takes a Message as an argument.&lt;/p&gt;

&lt;p&gt;The concepts of ‘channel membership’ and ‘messages’ are inherent parts of the Chat SDK.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Call the &lt;code&gt;getUnreadMessagesCount()&lt;/code&gt; API on the current user’s channel membership.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Also, the Chat SDK &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/features/messages/unread"&gt;unread messages&lt;/a&gt; documentation features an ‘Under the hood’ section, which explains how the Chat SDK is implemented… You will see that ‘under the hood’ the two implementations are identical.  &lt;strong&gt;This means neither approach is better from a technical standpoint, but the Chat SDK is easier to understand and more user-friendly&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with the PubNub Chat SDK
&lt;/h2&gt;

&lt;p&gt;The PubNub Chat SDK is available from yarn and can be installed as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add @pubnub/chat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please see our &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/sample-chat"&gt;Getting Started walkthrough&lt;/a&gt; or &lt;a href="https://youtu.be/_iHHCUn4aMU"&gt;video tutorial&lt;/a&gt; to start integrating the PubNub Chat SDK with your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources and Next Steps
&lt;/h2&gt;

&lt;p&gt;The following resources are available for our Chat SDK:&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation:
&lt;/h3&gt;

&lt;p&gt;Our documentation page contains a &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/build/configuration"&gt;full API reference&lt;/a&gt;, a &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/sample-chat"&gt;getting started walkthrough&lt;/a&gt;, and a primer to explain some of the &lt;a href="https://www.pubnub.com/docs/chat/chat-sdk/learn/glossary"&gt;concepts and terminology&lt;/a&gt; used by the SDK.&lt;/p&gt;

&lt;h3&gt;
  
  
  Demos:
&lt;/h3&gt;

&lt;p&gt;We have several pre-built demos, which can all be found in the &lt;a href="https://github.com/pubnub/js-chat/tree/master"&gt;js-chat&lt;/a&gt; monorepo on GitHub.  &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/pubnub/js-chat/tree/master/samples/react-native-group-chat"&gt;React Native Group Chat Demo&lt;/a&gt; is a fully featured showcase for the Chat SDK and should be your first stop to understand the capabilities of the API.  &lt;/p&gt;

&lt;p&gt;For a simple introduction to the Chat SDK, please see the &lt;a href="https://github.com/pubnub/js-chat/tree/master/samples/getting-started"&gt;Getting Started demo&lt;/a&gt;, which is referenced by the &lt;a href="https://pubnub.com/docs/chat/chat-sdk/build/sample-chat"&gt;documentation&lt;/a&gt; &amp;amp; &lt;a href="https://youtu.be/_iHHCUn4aMU"&gt;video tutorial&lt;/a&gt; and uses React. &lt;/p&gt;

&lt;p&gt;See the readme file associated with each demo for instructions on how to get started.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback Welcome
&lt;/h2&gt;

&lt;p&gt;We encourage you to download and try out the new SDK for your real-time applications and provide us with valuable feedback.  You can contact &lt;a href="https://support.pubnub.com/hc/en-us"&gt;PubNub support&lt;/a&gt; or the developer relations team directly at &lt;a href="//devrel@pubnub.com"&gt;devrel@pubnub.com&lt;/a&gt;.  Be on the lookout for additional Chat SDK resources in the future, and happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Socket Programming in Python: Client, Server, and Peer-to-Peer libraries</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Fri, 24 Feb 2023 11:56:21 +0000</pubDate>
      <link>https://forem.com/pubnub/socket-programming-in-python-client-server-and-peer-to-peer-libraries-1fjo</link>
      <guid>https://forem.com/pubnub/socket-programming-in-python-client-server-and-peer-to-peer-libraries-1fjo</guid>
      <description>&lt;p&gt;In this tutorial, you'll learn how to exchange data between a client and a server using Python socket programming and the Socket API.  Later, this tutorial will discuss exchanging data directly between two or more Python clients using a hosted provider.  The source code used in this tutorial can be found within the &lt;a href="https://github.com/PubNubDevelopers/python-socket-demo" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Socket programming connects two sockets (a client socket and a server socket) and allows them to communicate bi-directionally in real-time.  Direct socket connections can benefit all real-time applications since data can be sent or received anytime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment Set up
&lt;/h2&gt;

&lt;p&gt;You will need a stable version of &lt;a href="https://github.com/PubNubDevelopers/python-socket-demo" rel="noopener noreferrer"&gt;Python version 3.x&lt;/a&gt; installed on your machine.  If you are a Windows user, you have the option of adding Python to your PATH.  &lt;/p&gt;

&lt;p&gt;You will also need a code editor to follow along with this tutorial.  &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt; is a popular open-source and free code editor that supports many languages and frameworks, including Python.  VSCode also supports extensions for Python to help with code completion and debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build and run a Python socket application
&lt;/h2&gt;

&lt;p&gt;Let’s build a straightforward socket application using Python. Python provides a native &lt;a href="https://docs.python.org/3/library/socket.html" rel="noopener noreferrer"&gt;socket class&lt;/a&gt; (socket module), so developers don’t need to depend on external libraries. Begin by setting up the Python socket client and server:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjib5wlurr3dld0s982o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjib5wlurr3dld0s982o.png" alt="Python socket stack" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create the file &lt;code&gt;client.py&lt;/code&gt; in the project directory. To use sockets, import the Python socket library and create a new socket object that connects to a specified IP address (in this case, localhost on port number 8080, but you can select any ipv4 address).  Create a new connection to the socket server, send data to the TCP server, and close the socket connection.&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;client.py&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.0.0.0&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am CLIENT&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;from_server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will need a socket server to listen for incoming connections and messages from your client. Create the file &lt;code&gt;server.py&lt;/code&gt; and add the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;
&lt;span class="n"&gt;serv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;serv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.0.0.0&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;serv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;from_client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;''&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;from_client&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from_client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am SERVER&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;client disconnected and shutdown&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Server.py&lt;/code&gt; binds the socket object to the hostname (localhost) on port 8080 and continually listens for new client connections. When a client connects to this address, the server accepts the connection and reads any data. Once the data is successfully read from the client, the server provides a data response, at which point the client terminates the connection.&lt;/p&gt;

&lt;p&gt;To test this out yourself, open two terminal windows simultaneously.  In one window, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 server.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second window, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 client.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the server continues running and will establish a new connection every time you run the client and append any new output.&lt;/p&gt;

&lt;p&gt;The client will send the "I am CLIENT" string to the server and wait for a reply. The server will read the client’s message, output it to the terminal, and send back a response to the client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pcgkpf9acj6epj9jysr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pcgkpf9acj6epj9jysr.png" alt="Python Socket Example" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Socket Programming in Python using PubNub
&lt;/h2&gt;

&lt;p&gt;So far, this tutorial has covered exchanging messages between a server and a client, but what if you need to communicate directly between Python clients? &lt;/p&gt;

&lt;p&gt;Sending data directly between two or more client devices is tricky because you run into many scaling and security considerations as your number of devices increases.  A client-server architecture is used to moderate and manage your client-to-client communication. If you don’t have a web server or you worry about your server scaling to meet your application’s demands, you should opt for a hosted, real-time communication solution such as &lt;a href="https://www.pubnub.com/" rel="noopener noreferrer"&gt;PubNub&lt;/a&gt;. PubNub is a globally distributed and scalable cloud platform, so you don’t have to worry about deploying and maintaining servers.  PubNub’s cross-platform SDKs, including &lt;a href="https://www.pubnub.com/docs/sdks/python" rel="noopener noreferrer"&gt;Python&lt;/a&gt;, can identify users and send messages to specific channels, which only subscribed clients will receive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client-to-Client Python socket programming
&lt;/h3&gt;

&lt;p&gt;So, how would the simple app presented previously be written with PubNub to exchange messages directly between two clients?  It’s essential to understand that although PubNub uses the ‘publish’ and ‘subscribe’ architecture (pub/sub) to send and receive bidirectional messages between endpoints, it still uses sockets behind the scenes. PubNub gives you the benefits of socket communication without worrying about the details of Python network programming and maintaining an always-on connection between your clients regardless of the operating system.&lt;/p&gt;

&lt;p&gt;To integrate PubNub into the project, install the PubNub package with pip in the terminal; this will allow you to use the &lt;a href="https://www.pubnub.com/docs/sdks/python" rel="noopener noreferrer"&gt;PubNub Python SDK&lt;/a&gt; and communicate with the PubNub infrastructure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip3 &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="s1"&gt;'pubnub&amp;gt;=7.1.0'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will need to create two clients to connect to and communicate over the PubNub network. Create a file &lt;code&gt;pn_client_1.py&lt;/code&gt; and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pubnub.callbacks&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SubscribeCallback&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pubnub.enums&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PNStatusCategory&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pubnub.pnconfiguration&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PNConfiguration&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pubnub.pubnub&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PubNub&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="n"&gt;pnconfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PNConfiguration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__file__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pnconfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;publish_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;demo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;pnconfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subscribe_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;demo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;pnconfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;
&lt;span class="n"&gt;pnconfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ssl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;pubnub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PubNub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pnconfig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_publish_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;envelope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="c1"&gt;# Check whether request successfully completed or not
&lt;/span&gt;  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_error&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MySubscribeCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SubscribeCallback&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;presence&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;publisher&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;from device &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;publisher&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MySubscribeCallback&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chan-1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;## publish a message
&lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;exit&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;chan-1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;pn_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_publish_callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the file &lt;code&gt;pn_client_2.py&lt;/code&gt; and add the same code as you used for &lt;code&gt;pn_client_1.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The code above uses 'demo' keys, but you can obtain your custom &lt;a href="https://admin.pubnub.com/#/register" rel="noopener noreferrer"&gt;PubNub keys&lt;/a&gt; for free.  &lt;/p&gt;

&lt;p&gt;Run both &lt;code&gt;pn_client_1.py&lt;/code&gt; and &lt;code&gt;pn_client_2.py&lt;/code&gt; simultaneously in two different terminal windows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 pn_client_1.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 pn_client_2.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each client initializes its connection to the PubNub network and subscribes to receive new messages whenever they are published to the &lt;code&gt;chan-1&lt;/code&gt; channel. You can think of this as sending data over a TCP socket in Python; behind the scenes, PubNub is creating and managing the socket for you and routing your message to all clients who are listening for it. Once the remote client receives the message, the received message is displayed on the command line.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folrg6dfdahjpv6fywktw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Folrg6dfdahjpv6fywktw.png" alt="Python PubNub example" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s all there is to it! For more information on developing with PubNub, check out their range of &lt;a href="https://www.pubnub.com/tutorials/" rel="noopener noreferrer"&gt;tutorials&lt;/a&gt; and &lt;a href="https://www.pubnub.com/demos/" rel="noopener noreferrer"&gt;demos&lt;/a&gt;.  Alternatively, check out the &lt;a href="https://www.pubnub.com/tour/introduction/" rel="noopener noreferrer"&gt;PubNub interactive live tour&lt;/a&gt; to understand how the platform provides real-time interactivity to applications. PubNub supports TCP and UDP datagram communication, as well as Linux, Unix, and Windows.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>mcp</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>WebSocket Programming Examples with and without Node.js</title>
      <dc:creator>darryncampbell</dc:creator>
      <pubDate>Fri, 24 Feb 2023 11:56:14 +0000</pubDate>
      <link>https://forem.com/pubnub/websocket-programming-examples-with-and-without-nodejs-22nj</link>
      <guid>https://forem.com/pubnub/websocket-programming-examples-with-and-without-nodejs-22nj</guid>
      <description>&lt;p&gt;If you are reading this, you are probably interested in creating an application or service that needs two-way communication that either side can initiate. Node JavaScript (Node.js) can be used to quickly develop and deploy this application using WebSockets, which were designed with this use case in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a WebSocket?
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://www.pubnub.com/learn/glossary/what-is-websocket/" rel="noopener noreferrer"&gt;WebSocket&lt;/a&gt; is a computer communications protocol providing &lt;a href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)" rel="noopener noreferrer"&gt;duplex&lt;/a&gt; communication channels over a single TCP connection. In other words, it allows internet-capable devices to communicate with each other, one acting as the client and the other acting as the server, with both able to initiate communication.&lt;/p&gt;

&lt;h2&gt;
  
  
  What types of applications use WebSockets?
&lt;/h2&gt;

&lt;p&gt;The WebSocket protocol is used wherever a solution requires real-time communication and example use cases include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pubnub.com/use-case/in-app-chat/" rel="noopener noreferrer"&gt;Chat Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pubnub.com/use-case/geolocation/" rel="noopener noreferrer"&gt;Live Location Tracking on a Map&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pubnub.com/solutions/live-audience-engagement/" rel="noopener noreferrer"&gt;Live Audience Interaction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pubnub.com/solutions/iot-device-control/" rel="noopener noreferrer"&gt;IoT Device Updates&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WebSockets create a TCP socket connection between multiple devices or processes. As will be discussed later, similar functionality can be implemented using &lt;a href="https://www.pubnub.com/learn/glossary/http-long-polling/" rel="noopener noreferrer"&gt;HTTP Long Polling&lt;/a&gt; or a hosted pub/sub service, but let’s build a simple example first using WebSockets and Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js WebSocket API Example - a basic chat application
&lt;/h2&gt;

&lt;p&gt;All source code associated with this blog is hosted on &lt;a href="https://github.com/PubNubDevelopers/nodejs-websocket-examples" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To build a basic chat application with WebSockets, you will need both a client and server component.&lt;/p&gt;

&lt;p&gt;For the server, we will use Node.js, and the client-side code will run within a web browser such as Chrome. &lt;/p&gt;

&lt;h3&gt;
  
  
  Node.js WebSockets Code (Server)
&lt;/h3&gt;

&lt;p&gt;This application will require both a web server (HTTP server) and a WebSocket server (wss).  The web server allows us to load our client (running in a browser), and the WebSocket server handles the bidirectional chat messages.&lt;/p&gt;

&lt;p&gt;Create the Node.js app and install both the Express.js and ‘ws’ packages which will provide our web server and WebSocket server, respectively. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm init
&lt;span class="k"&gt;**&lt;/span&gt;Follow the prompts and accept the defaults&lt;span class="k"&gt;**&lt;/span&gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; express
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; ws


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The web server portion will serve a single web page to the client, websocket-client.html, on port 3000:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webserver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
   &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/websocket-client.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Listening on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A WebSocket server can be created in only a few lines using the Node.js WebSocket library (ws) package:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WebSocketServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sockserver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocketServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After creating an instance of a WebSocket server and specifying the port to run on, we can define any action that should happen after the WebSocket connection is established.  In our case, we write connection events to the console and forward any messages we receive to previously connected clients. WebSockets are designed to run on the same ports as HTTP/HTTPS (i.e., 80 and 443).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;sockserver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New client connected!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection established&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;close&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Client has disconnected!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;sockserver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`distributing message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;websocket error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And with that, the server portion is complete. Every time a message is received on any socket, it proxies the message to every connected client, which is the basis of any group-chat app.&lt;/p&gt;

&lt;p&gt;The full &lt;code&gt;index.js&lt;/code&gt; code looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webserver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
   &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/websocket-client.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Listening on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WebSocketServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sockserver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocketServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;sockserver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New client connected!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection established&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;close&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Client has disconnected!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;sockserver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`distributing message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;
 &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;websocket error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And you can run it from the command line as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

node index.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Messages can either be text or JSON encoded (JSON.stringify). This code uses the utf-8 charset by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  WebSockets Code (Client / Browser)
&lt;/h3&gt;

&lt;p&gt;We don’t have to install additional software or packages to use WebSockets with modern web browsers. Create a listening WebSocket by providing a correctly formatted URI:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webSocket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws://localhost:443/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And defining an event handler for when a message is received from the server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; 
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from server: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That’s it.  We can now receive data from the WebSocket server.&lt;br&gt;
To send a message event from the client, you send() on the socket object:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To make our chat app functional, we just need to add an input field and a ‘send message’ button, so your frontend HTML code should look something like this (styling &amp;amp; css omitted for brevity):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"input-form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Enter Message:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Send"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"messages"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webSocket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws://localhost:443/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from server: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;open&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We are connected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;inputMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;
      &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input-form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Make sure you name your HTML page &lt;code&gt;websocket-client.html&lt;/code&gt; and launch a few tabs in your browser, navigating to &lt;code&gt;http://localhost:3000&lt;/code&gt;.  You should see messages received in every tab:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe55h65rr15n9yjc140k3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe55h65rr15n9yjc140k3.png" alt="Simple chat app using WebSockets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the same chat app without a Node.js server
&lt;/h2&gt;

&lt;p&gt;If you don’t have a server or have concerns about scaling your server infrastructure to meet your application’s future demands, you should opt for a hosted real-time communication solution such as Socket.io or &lt;a href="https://www.pubnub.com/" rel="noopener noreferrer"&gt;PubNub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;PubNub is a globally distributed and scalable cloud platform, so you do not have to worry about deploying and maintaining servers on your backend.  &lt;a href="https://www.pubnub.com/docs/sdks" rel="noopener noreferrer"&gt;PubNub SDKs&lt;/a&gt; can identify users and send messages to specific channels, which only subscribed users will receive.&lt;/p&gt;

&lt;p&gt;So, how would the simple chat app presented above be written with PubNub?  One benefit of PubNub is that it is &lt;a href="https://support.pubnub.com/hc/en-us/articles/360051496552-Does-PubNub-use-WebSockets-or-some-other-protocol-" rel="noopener noreferrer"&gt;protocol agnostic&lt;/a&gt;.  PubNub uses the ‘publish’ and ‘subscribe’ architecture (pub/sub) to send and receive bidirectional messages.&lt;/p&gt;

&lt;p&gt;First, include the PubNub JavaScript SDK in the header, available from their CDN:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.pubnub.com/sdk/javascript/pubnub.7.2.2.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To subscribe for incoming messages, create a PubNub instance, define the channel, and add a listener:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;pubnub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PubNub&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;publishKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;demo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;subscribeKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;demo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws-channel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addListener&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; 
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from client: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The page will be updated whenever messages are received on the &lt;code&gt;ws-channel&lt;/code&gt;.  The code above uses 'demo' keys, but you can obtain your custom &lt;a href="https://admin.pubnub.com/#/register" rel="noopener noreferrer"&gt;PubNub keys&lt;/a&gt; for free. &lt;/p&gt;

&lt;p&gt;To send messages over PubNub, publish on the same channel you subscribed to previously, in this case:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws-channel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The channel model is very flexible and extensible, but for this simple example, it is sufficient to just send and receive messages on the same channel.  Your message will be delivered anywhere in the world in under 100ms.  &lt;/p&gt;

&lt;p&gt;Since there is no server component required with a PubNub deployment, the entire application, written in HTML and JS, is contained within a single file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.pubnub.com/sdk/javascript/pubnub.7.2.2.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;H1&amp;gt;&lt;/span&gt;This code uses PubNub, which is protocol agnostic&lt;span class="nt"&gt;&amp;lt;/H1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"input-form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Enter Message:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Send"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"messages"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;pubnub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PubNub&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;publishKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;demo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;subscribeKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;demo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws-channel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addListener&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; 
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Message from client: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;inputMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;pubnub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ws-channel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;input-form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can see a live version of this code at&lt;br&gt;
&lt;a href="https://pubnubdevelopers.github.io/nodejs-websocket-examples/pubnub-client.html" rel="noopener noreferrer"&gt;https://pubnubdevelopers.github.io/nodejs-websocket-examples/pubnub-client.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqtu51p9ym3fgxozcrk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqtu51p9ym3fgxozcrk8.png" alt="Simple chat app using PubNub"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s all there is to it! For more information on developing with PubNub, check out their range of &lt;a href="https://www.pubnub.com/tutorials/" rel="noopener noreferrer"&gt;tutorials&lt;/a&gt; and &lt;a href="https://www.pubnub.com/demos/" rel="noopener noreferrer"&gt;demos&lt;/a&gt;.  Alternatively, check out the &lt;a href="https://www.pubnub.com/tour/introduction/" rel="noopener noreferrer"&gt;PubNub interactive live tour&lt;/a&gt; to understand how the platform provides real-time interactivity to applications.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
