How to Build a Concurrent Chat App with Go and Websockets

How to Build a Concurrent Chat App with Go and Websockets

Go emerged from Google out of a need to build highly performant applications using an easy-to-understand syntax. It's a statically typed, compiled language developed by some of the innovators of C, without the programming burden of manual memory management. Primarily, it was designed to take advantage of modern multicore CPUs and networked machines.

Go emerged from Google out of a need to build highly performant applications using an easy-to-understand syntax. It's a statically typed, compiled language developed by some of the innovators of C, without the programming burden of manual memory management. Primarily, it was designed to take advantage of modern multicore CPUs and networked machines.

In this post, I'll demonstrate the capabilities of Go. We'll take advantage of Go's ability to easily create concurrent apps to build a chat app. On the backend, we'll use Redis as the intermediary to accept messages from the browser and send them to the subscribed clients. On the frontend, we'll use websockets via  socket.io to facilitate the client-side communication. We'll deploy it all on  Heroku, a PaaS provider that makes it easy to deploy and host your apps. Just as Go makes programming such an application simple, Heroku makes it easy to supplement it with additional infrastructure.

Channels in Go

What developers find appealing about Go is its ability to communicate concurrently, which it does through a system called channels. It's important to draw upon  an oft-cited distinction between concurrency and parallelism. Parallelism is the process by which a CPU executes multiple tasks at the same time, while concurrency is the CPU's ability to switch between multiple tasks, which start, run, and complete while overlapping one another. In other words, parallel programs handle many operations at once, while concurrent programs can switch between many operations over the same period of time.

A channel in Go is the conduit through which concurrency flows. Channels can be unidirectional—with data either sent to or received by them—or bidirectional, which can do both. Here's an example that demonstrates the basic principles of concurrency and channels:

 func one(c1 chan string) {   
  for i := 0; i < 5; i++ {     
   c1 <- "Channel One"   
  }   
  close(c1) 
 } 
 func two(c2 chan string) {   
  for i := 0; i < 5; i++ {     
   c2 <- "Channel Two"   
  }   
  close(c2) } 
 func main() {   
  c1 := make(chan string)   
  c2 := make(chan string)   
  go one(c1)   
  go two(c2)   
  for {    
   select {       
    case msg, ok := <-c1:      
    fmt.Println(msg)       
    if !ok {        
     c1 = nil      
    }       
    case msg, ok := <-c2:       
    fmt.Println(msg)       
    if !ok {        
     c2 = nil      
    }     
 }    
  if c1 == nil && c2 == nil {       
     break     
  }   
 } 
}

golang websockets tutorial

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Data Visualization in R with ggplot2: A Beginner Tutorial

Data Visualization in R with ggplot2: A Beginner Tutorial. Learn to visualize your data using R and ggplot2 in this beginner-friendly tutorial that walks you through building a chart for data analysis.

Tutorial: Getting Started with R and RStudio

Get your R programming journey off on the right foot with this RStudio tutorial that walks through everything from installation to best practices.

Tutorial: Loading and Cleaning Data with R and the tidyverse

Learn how to load a data set and clean it using R programming and tidyverse tools in this free beginner-level data analysis tutorial.

R Tutorial: Better Blog Post Analysis with googleAnalyticsR

Learn to compare blog posts on even footing using R programming and the googleAnalyticsR package for blog data analysis in this free tutorial.

Go Tutorial (Golang) 3 - Golang Variables - Declaring Variables in Go

Welcome to this course on Go Programming Language Tutorial. Go is an open source programming language which was originally developed by Google. In this Go Tu...